req.query
This property is used to access the request query
parameters.
Example
var name = req.query.name
var city = req.query.city
index.js
const express = require('express') var cookieParser = require('cookie-parser') const app = express() app.use(cookieParser()); app.get('/', (req, res) => res.send(`Request protocol ${req.protocol}`)) app.get('/user', (req, res) => { var name = req.query.name var city = req.query.city if(req.query.user){ var height = req.query.user.height var color = req.query.user.color } var receivedData = ` name : ${name} <br /> city : ${city} <br /> height : ${height} <br /> color : ${color} <br /> ` res.send(receivedData) }) const port = 3000 app.listen(port, () => console.log(`Application started listening on port ${port}!`));
Run index.js
Open the url ‘http://localhost:3000/user?name=krishna’,
you will see below kind of response.
Open the url ‘http://localhost:3000/user?name=krishna&city=Bangalore’,
you will see below response.
Open the url ‘http://localhost:3000/user?name=krishna&city=Bangalore&user[height]=6.5&user[color]=black’
You will see below response.
No comments:
Post a Comment