How to pass request query parameters through a Node js application

0 votes

How to pass request query parameters through a Node.js application?

Describe how to handle query parameters in a Node.js application. Mention using frameworks like Express to retrieve parameters from the request object (req.query) and pass them to functions, APIs, or databases as needed. Highlight structuring the URL to include key-value pairs for queries.

Nov 26, 2024 in Node-js by Nidhi
• 8,520 points
79 views

1 answer to this question.

0 votes

Using Node.js http Module

Step 1: Basic Server Using http Module

const http = require('http');

const url = require('url');

const server = http.createServer((req, res) => {

  // Parse the URL to get query parameters

  const parsedUrl = url.parse(req.url, true); // The `true` argument parses query string into an object

  const queryParameters = parsedUrl.query;

// Get query parameters

  const query = queryParameters.query;

  const page = queryParameters.page;

 // Set response headers

  res.writeHead(200, { 'Content-Type': 'application/json' });

 // Send a response

  res.end(JSON.stringify({

    message: `Searching for "${query}" on page ${page}`

  }));

});

server.listen(5000, () => {

  console.log('Running server on http://localhost:5000');

});

In this example:

A GET request to /search?query=nodejs&page=1 would also return a JSON response:

{  "message": "Searching for 'nodejs' on page 1"}

answered Dec 13, 2024 by Navya

Related Questions In Node-js

0 votes
1 answer

How to host a Node.Js application in shared hosting?

Hello @kartik, You can run node.js server on a typical ...READ MORE

answered Jul 17, 2020 in Node-js by Niroj
• 82,840 points
7,369 views
0 votes
1 answer

How to update a value in a json file and save it through node.js?

Hello @kartik, It's particularly useful if you're concerned ...READ MORE

answered Oct 12, 2020 in Node-js by Niroj
• 82,840 points
26,114 views
0 votes
1 answer

How to get data out of a Node.js http get request?

Hello @kartik, Your logs return undefined : you log before ...READ MORE

answered Oct 12, 2020 in Node-js by Niroj
• 82,840 points
7,164 views
0 votes
1 answer

How to get GET (query string) variables in Express.js on Node.js?

Hello @kartik, Since you've mentioned Express.js in your ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
3,352 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
2,015 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,551 views
0 votes
1 answer

Start script missing error when running npm start

It seems that there is an undefined ...READ MORE

answered Feb 10, 2022 in Java by Soham
• 9,710 points
4,547 views
0 votes
1 answer

How do I use ES6 features like destructuring in a Node.js application?

To use ES6 features like destructuring in ...READ MORE

answered Dec 17, 2024 in Node-js by Navya
92 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP