req.query
is a request object that is populated by request query strings that are found in a URL. These query strings are in key-value form. They start after the
https://educative.io/user?name=Theodore&isAuthor=true
From the above code, the query strings are name
and isAuthor
. When this request is made, the req.query
object becomes populated with the query strings.
req.query = {name: "Theodore", isAuthor: true}
expressJs
var express = require('express');var app = express();const PORT = 3000;app.listen(PORT, ()=>{console.log("app running on port "+PORT);})
app.get("/user", (req, res)=>{var name = req.query.name;var isAuthor = req.query.isAuthor;res.json({name,isAuthor})})
/user?name=Theodore&isAuthor=true"
.{
"name": "Theodore",
"isAuthor": "true"
}
Thanks for reading!