“Expressjs Param” Réponses codées

Params Express JS

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
});
Ham-Solo

Express Get URL Paramètres

app.get('/path/:name', function(req, res) {
  res.send("tagId is set to " + req.params.name);
});
Batman

Param exprime dans l'URL

app.get('/p/:tagId', function(req, res) {
  res.send("tagId is set to " + req.params.tagId);
});

// GET /p/5
// tagId is set to 5
SirSundays

Node Express Params

// 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
})
miletoo

Expressjs Param

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()
})
ibsifat

Réponses similaires à “Expressjs Param”

Questions similaires à “Expressjs Param”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code