Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Can we get the variables in the query string in Node.js just like we get them in $_GET in PHP?

I know that in Node.js we can get the URL in the request. Is there a method to get the query string parameters?

like image 481
XMen Avatar asked Aug 02 '11 13:08

XMen


People also ask

How do I parse a query string in node JS?

To parse query string in Node. js, we can use the url module. const http = require('http'); const url = require('url'); const server = http. createServer((request, response) => { const { query: queryData } = url.


1 Answers

Since you've mentioned Express.js in your tags, here is an Express-specific answer: use req.query. E.g.

var express = require('express'); var app = express();  app.get('/', function(req, res){   res.send('id: ' + req.query.id); });  app.listen(3000); 
like image 76
whitequark Avatar answered Sep 24 '22 11:09

whitequark