Created
February 26, 2012 17:11
-
-
Save clonn/1917811 to your computer and use it in GitHub Desktop.
Node.js express simple route http server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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