mirror of
https://github.com/expressjs/express.git
synced 2026-02-21 19:41:36 +00:00
renamed example to params
This commit is contained in:
64
examples/params/app.js
Normal file
64
examples/params/app.js
Normal file
@@ -0,0 +1,64 @@
|
||||
|
||||
// Expose modules in ./support for demo purposes
|
||||
require.paths.unshift(__dirname + '/../../support');
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var express = require('../../lib/express')
|
||||
, app = express.createServer();
|
||||
|
||||
// Faux database
|
||||
|
||||
var users = [
|
||||
{ name: 'tj' }
|
||||
, { name: 'tobi' }
|
||||
, { name: 'loki' }
|
||||
, { name: 'jane' }
|
||||
, { name: 'bandit' }
|
||||
];
|
||||
|
||||
// Convert :to and :from to integers
|
||||
|
||||
app.param(['to', 'from'], function(n){ return parseInt(n, 10); });
|
||||
|
||||
// Load user by id
|
||||
|
||||
app.param('user', function(req, res, next, id){
|
||||
if (req.user = users[id]) {
|
||||
next();
|
||||
} else {
|
||||
next(new Error('failed to find user'));
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* GET index.
|
||||
*/
|
||||
|
||||
app.get('/', function(req, res){
|
||||
res.send('Visit /user/0 or /users/0-2');
|
||||
});
|
||||
|
||||
/**
|
||||
* GET :user.
|
||||
*/
|
||||
|
||||
app.get('/user/:user', function(req, res, next){
|
||||
res.send('user ' + req.user.name);
|
||||
});
|
||||
|
||||
/**
|
||||
* GET users :from - :to.
|
||||
*/
|
||||
|
||||
app.get('/users/:from-:to', function(req, res, next){
|
||||
var from = req.params.from
|
||||
, to = req.params.to
|
||||
, names = users.map(function(user){ return user.name; });
|
||||
res.send('users ' + names.slice(from, to).join(', '));
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
console.log('Express application listening on port 3000');
|
||||
Reference in New Issue
Block a user