Skip to content

Instantly share code, notes, and snippets.

@clonn
Created February 26, 2012 17:11
Show Gist options
  • Save clonn/1917811 to your computer and use it in GitHub Desktop.
Save clonn/1917811 to your computer and use it in GitHub Desktop.
Node.js express simple route http server
/**
* @overview
*
* @author Caesar Chi
* @blog clonn.blogspot.com
* @version 2012/02/26
*/
// create server.
var app = require('express').createServer(),
port = 1337;
app.listen(port);
// normal style
app.get('/', function(req, res){
res.send('hello world');
});
app.get('/test', function(req, res){
res.send('test render');
});
// parameter style
app.get('/user/:id', function(req, res){
res.send('user: ' + req.params.id);
});
app.get('/:number', function(req, res){
res.send('number: ' + req.params.number);
});
// REGX style
app.get(/^\/ip?(?:\/(\d{2,3})(?:\.(\d{2,3}))(?:\.(\d{2,3})))?/, function(req, res){
res.send(req.params);
});
app.get('*', function(req, res){
res.send('Page not found!', 404);
});
console.log('start express server\n');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment