Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,872 questions

51,796 answers

573 users

How to parse url and get the query values in Node.js

3 Answers

0 votes
const http = require('http');
const url = require('url');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  var q = url.parse(req.url, true).query;
  var t = q.age + " " + q.name;
  res.end(t);
}).listen(8080); 



// To run open http://localhost:8080/?age=48&name=Tom in your web browser

/*
run:

48 Tom

*/

 



answered Feb 27, 2020 by avibootz
edited Mar 1, 2020 by avibootz
0 votes
const http = require('http');
const url = require('url');
 
http.createServer(function (req, res) {
    const queryObject = url.parse(req.url,true).query;
    console.log(queryObject);

    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end("end");
}).listen(8080); 
 
 

 
// To run open http://localhost:8080/?age=48&name=Tom in your web browser
 
/*
run:
 
[Object: null prototype] { age: '48', name: 'Tom' } // output (console)

end // in web browser
 
*/

 



answered Feb 27, 2020 by avibootz
0 votes
const http = require('http');
const url = require('url');
 
http.createServer(function (req, res) {
    const queryObject = url.parse(req.url,true).query;
    console.log(queryObject.name);
    console.log(queryObject.age);

    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end("end");
}).listen(8080); 
 
 

 
// To run open http://localhost:8080/?age=48&name=Tom in your web browser
 
/*
run:
 
Tom // output (console)s
48 // output (console)

end // in web browser
 
*/
 

 



answered Feb 27, 2020 by avibootz

Related questions

3 answers 246 views
1 answer 199 views
2 answers 197 views
197 views asked Feb 27, 2020 by avibootz
2 answers 239 views
1 answer 230 views
1 answer 118 views
...