Middleware - 401-advanced-javascript-v/seattle-javascript-401d29 GitHub Wiki

Route with Parameters /hello/:name

Code example

// server.js

...

// we'll create our routes here

// get an instance of router var router = express.Router();

...

// route with parameters (http://localhost:8080/hello/:name) router.get('/hello/:name', function(req, res) { res.send('hello ' + req.params.name + '!'); });

// apply the routes to our application app.use('/', router);

Route Middleware for Parameters

// server.js

...

// we'll create our routes here

// get an instance of router var router = express.Router();

...

// route middleware to validate :name router.param('name', function(req, res, next, name) { // do validation on name here // blah blah validation // log something so we know its working console.log('doing name validations on ' + name);

`// once validation is done save the new item in the req`
`req.name = name;`
`// go to the next thing`
`next(); `

});

// route with parameters (http://localhost:8080/hello/:name) router.get('/hello/:name', function(req, res) { res.send('hello ' + req.name + '!'); });

// apply the routes to our application app.use('/', router);

...