1app.get('/path/:name', function(req, res) { // url: /path/test
2 console.log(req.params.name); // result: test
3});
4
5// OR
6
7app.get('/path', function(req, res) { // url: /path?name='test'
8 console.log(req.query['name']); // result: test
9});
1GET /something?color1=red&color2=blue
2
3app.get('/something', (req, res) => {
4 req.query.color1 === 'red' // true
5 req.query.color2 === 'blue' // true
6})
7
8req.params refers to items with a ':' in the URL and req.query refers to items associated with the '?
1app.get('/path/:name', function(req, res) {
2 res.send("tagId is set to " + req.params.name);
3});
1app.get('/p/:tagId', function(req, res) {
2 res.send("tagId is set to " + req.params.tagId);
3});
4
5// GET /p/5
6// tagId is set to 5
7
1app.get('/users/:userId/books/:bookId', function (req, res) {
2 res.send(req.params)
3})
4
1Route path: /users/:userId/books/:bookId
2Request URL: http://localhost:3000/users/34/books/8989
3req.params: { "userId": "34", "bookId": "8989" }
4