Friday 8 March 2019

Express: req.param(name [, defaultValue]): Return value of the parameter


req.param(name [, defaultValue])
Return the value of the parameter ‘name’. if the parameter not found, then it returns the provided default value.

Parameters are searched in following order.
a.   req.params
b.   req.body
c.    req.query

index.js
'use strict'

const express = require('express')

const app = express()

app.get('/', (req, res) => {
 res.send('Hello World')
})

app.get('/user/:userId/details', (req, res) => {
 var userId = req.param('userId')
 var detailsType = req.param('detailsType', 'minimal')
 
 res.send(`userId : ${userId} <br />
 detailsType : ${detailsType}`)
 
})

const port = 3000
app.listen(port, () => console.log(`Application started listening on port ${port}!`));

Run index.js.


Open the url ‘http://localhost:3000/user/krishna/details’ in browser, you can see below message.



Hit the url ‘http://localhost:3000/user/krishna/details?detailsType=all’, you will see below message.




Previous                                                 Next                                                 Home

No comments:

Post a Comment