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})
1var express = require('express');
2var app = express();
3
4app.get('/', function(req, res){
5 res.send('id: ' + req.query.id);
6});
7
8app.listen(3000);
1// GET /search?q=tobi+ferret
2console.dir(req.query.q)
3// => 'tobi ferret'
4
5// GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse
6console.dir(req.query.order)
7// => 'desc'
8
9console.dir(req.query.shoe.color)
10// => 'blue'
11
12console.dir(req.query.shoe.type)
13// => 'converse'
14
15// GET /shoes?color[]=blue&color[]=black&color[]=red
16console.dir(req.query.color)
17// => ['blue', 'black', 'red']
18
1//get query¶ms in express
2
3//etc. example.com/user/000000?sex=female
4
5app.get('/user/:id', function(req, res) {
6 const query = req.query;// query = {sex:"female"}
7 const params = req.params; //params = {id:"000000"}
8
9})
10