when i sent a request to nodejs server,
how can we find the parameters sent in the request query when request sent to nodejs server.
req.param
req.params
req.query
all giving undefined.
also when i stringify
req
request it gives error :
Converting circular structure to JSON
How to find query parameters.
The query parameter is the variable whose value is passed in the URL in the form of key-value pair at the end of the URL after a question mark (?). For example, www.geeksforgeeks.org? name=abc where, 'name' is the key of query parameter whose value is 'abc'.
Your query parameters can be retrieved from the query object on the request object sent to your route. It is in the form of an object in which you can directly access the query parameters you care about. In this case Express handles all of the URL parsing for you and exposes the retrieved parameters as this object.
Get a single Query String value import React from 'react'; import { useSearchParams } from 'react-router-dom'; const Users = () => { const [searchParams] = useSearchParams(); console. log(searchParams); // ▶ URLSearchParams {} return <div>Users</div>; };
You can use the url module:
$ npm install url
And then something like this:
var http = require("http");
var url = require("url");
http.createServer(function(req, res) {
var parsedUrl = url.parse(req.url, true); // true to get query as object
var queryAsObject = parsedUrl.query;
console.log(JSON.stringify(queryAsObject));
res.end(JSON.stringify(queryAsObject));
}).listen(8080);
console.log("Server listening on port 8080");
Test in your browser:
http://localhost:8080/?a=123&b=xxy
For POST requests you can use bodyParser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With