app.get('/path/:name', function(req, res) { // url: /path/test
console.log(req.params.name); // result: test
});
// OR
app.get('/path', function(req, res) { // url: /path?name='test'
console.log(req.query['name']); // result: test
});
GET /something?color1=red&color2=blue
app.get('/something', (req, res) => {
req.query.color1 === 'red' // true
req.query.color2 === 'blue' // true
})
req.params refers to items with a ':' in the URL and req.query refers to items associated with the '?
app.get('/p/:tagId', function(req, res) {
res.send("tagId is set to " + req.params.tagId);
});
// GET /p/5
// tagId is set to 5
// url = /something/2?color1=red&color2=blue&type=square
app.get('/something/:id', (req, res) => {
req.params.id === 2 // true
req.query.color1 === 'red' // true
req.query.color2 === 'blue' // true
req.query.type === 'square' // true
})
app.param(['id', 'page'], function (req, res, next, value) {
console.log('CALLED ONLY ONCE with', value)
next()
})
app.get('/user/:id/:page', function (req, res, next) {
console.log('although this matches')
next()
})
app.get('/user/:id/:page', function (req, res) {
console.log('and this matches too')
res.end()
})