High performance, high class web development for Node.js
The express.Server now inherits from http.Server, however follows the same idiom by providing express.createServer() as shown below. This means that you can utilize Express server's transparently with other libraries.
var app = require('express').createServer();
app.get('/', function(req, res){
res.send('hello world');
});
app.listen(3000);
Express supports arbitrary environments, such as production and development. Developers can use the configure() method to setup needs required by the current environment. When configure() is called without an environment name it will be run in every environment prior to the environment specific callback.
In the example below we only dumpExceptions, and respond with exception stack traces in development mode, however for both environments we utilize methodOverride and bodyDecoder.
app.configure(function(){
app.use('/', connect.methodOverride());
app.use('/', connect.bodyDecoder());
});
app.configure('development', function(){
app.use('/', connect.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use('/', connect.errorHandler());
});
For internal and arbitrary settings Express provides the set(key[, val]), enable(key), disable(key) methods:
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('views');
// => "... views directory ..."
app.enable('some feature');
// same as app.set('some feature', true);
app.disable('some feature');
// same as app.set('some feature', false);
});
Express supports the following settings out of the box:
Express utilizes the HTTP verbs to provide a meaningful, expressive routing API. For example we may want to render a user's account for the path /user/12, this can be done by defining the route below. The values associated to the named placeholders, are passed as the third argument, which here we name params.
app.get('/user/:id', function(req, res, params){
res.send('user ' + params.id);
});
A route is simple a string which is compiled to a RegExp internally. For example when /user/:id is compiled, a simplified version of the regexp may look similar to:
\/user\/([^\/]+)\/?
Literal regular expressions may also be passed for complex uses:
app.get(/^\/foo(bar)?$/, function(){});
Below are some route examples, and the associated paths that they may consume:
"/user/:id"
/user/12
"/users/:id?"
/users/5
/users
"/files/*"
/files/jquery.js
/files/javascripts/jquery.js
"/file/*.*"
/files/jquery.js
/files/javascripts/jquery.js
"/user/:id/:operation?"
/user/1
/user/1/edit
"/products.:format"
/products.json
/products.xml
"/products.:format?"
/products.json
/products.xml
/products
We may pass control to the next matching route, by calling the fourth parameter, the next() function. When a match cannot be made, control is passed back to Connect.
app.get('/users/:id?', function(req, res, params){
if (params.id) {
// do something
} else {
next();
}
});
app.get('/users', function(req, res, params){
// do something else
});
The Express Plugin is no more! middleware via Connect can be passed to express.createServer() as you would with a regular Connect server. For example:
var connect = require('connect'),
express = require('express');
var app = express.createServer(
connect.logger(),
connect.redirect(),
connect.bodyDecoder()
);