ExpressJS - Guide1

expressjs

http://webapplog.com/express-js-fundamentals/ - done reading

How can we install express?

Assume that you have installed Node and NPM, you can install Express:

sudo npm install -g express@3.4.3

How can we create an application?

$ express -c styl expressfun
$ cd expressfun && npm install
$ node app

The above code creates an application named expressfun. The second line cd into the expressfun application directory and install the basic dependent modules. The third line start the application server.

You can now open a browser and go to http://localhost:3000

The full code for expressfun/app.js:

// load built-in or global module:
var express = require('express');
var http = require('http');
var path = require('path');

// Create the application instance:
var app = express();

// load various middleware:
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());

// define the static directory
app.use(express.static(path.join(__dirname, 'public')));

// set the port number
app.set('port', process.env.PORT || 3000);

// set the view template engine
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

app.use(app.router);

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

// load application specific modules
var routes = require('./routes');
var user = require('./routes/user');

app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License