Express provides several different "parameters" objects:
req.params
for path parameters (aka route parameters) signified with a :
in the route matcherreq.query
for query parameters which appear after the ?
in the URLreq.body
for post parameters which appear inside the request bodyThe special character :
means "this is a path parameter"
Example:
Path: /hello/Gandalf
Route: /hello/:name
Params: {name: 'Gandalf'}
Express will grab the value from the path itself, and put it into the request.params
object for you to use later.
For query parameters like ?season=winter&weather=cold
Express will grab the name and value from the query string, and put it into the request.query
object for you to use later
Now change your "Hello, Express" server to have the following route:
app.get('/hello', (request, response)=> {
response.send('Hello, ' + request.query.friend + '!')
});
Prove that it works by visiting http://localhost:5000/hello?friend=Gandalf (or use your own name)
Since request bodies can appear in several different formats, you need to use the correct middleware to extract them.
express.urlencoded
parses incoming requests with URL-encoded payloadsexpress.json
parses incoming requests with JSON payloadsExample (from the express guide):
// POST /login gets urlencoded bodies
app.post('/login', express.urlencoded(), function (req, res) {
res.send('welcome, ' + req.body.username)
})
// POST /api/users gets JSON bodies
app.post('/api/users', express.json(), function (req, res) {
// create user in req.body
})
/