Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find request parameters in 'nodejs'

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.

like image 356
codeofnode Avatar asked Sep 04 '13 11:09

codeofnode


People also ask

How do I read a query parameter in node JS?

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'.

How can you capture query params sent by GET method?

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.

How do I get parameters in react js?

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>; };


1 Answers

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.

like image 174
Jazor Avatar answered Oct 26 '22 08:10

Jazor