Thursday, 9 April 2015

Node js Express js get parameters from request


1. get "123" out from  "xx/xx/123"

you need "req.params.balabala", but first you should do something in your "route":

router.get('xx/xx/:balabala', controller.xxxxxxxxx);




2. get "peter", "18" out from  "xx/xx/123?name=peter&age=18"

you need req.query.

req.query
An object containing a property for each query string parameter in the route. If there is no query string, it is the empty object, {}.
// GET /search?q=tobi+ferret
req.query.q
// => "tobi ferret"

// GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse
req.query.order
// => "desc"

req.query.shoe.color
// => "blue"

req.query.shoe.type
// => "converse"


3. if you want get data from html form:

req.body

Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser and multer.
This example shows how to use body-parsing middleware to populate req.body.

No comments:

Post a comment