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

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,449 answers

573 users

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

2 Answers

0 votes
var url = require('url');

const myURL = 'https://www.website.com/orders/index.html?id=8372&age=43';
 
var url_parts = url.parse(myURL, true);

var query = url_parts.query;

console.log(query.id);
console.log(query.age);

 
    
/*
run:
  
8372
43
  
*/

 



answered Mar 22, 2020 by avibootz
0 votes
var url = require('url');
const http = require('http');
  
const PORT = process.env.PORT || 8080;

http.createServer((req, res) => {
    const queryObject = url.parse(req.url,true).query;
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(queryObject.age + ' ' + queryObject.name);
}).listen(PORT, () => console.log(`Server started on port ${PORT}`)); 


    
/*
run:
  
Server started on port 8080

On http://localhost:8080/?age=48&name=Tom
48 Tom

*/

 



answered Mar 22, 2020 by avibootz

Related questions

3 answers 327 views
1 answer 241 views
3 answers 415 views
1 answer 172 views
3 answers 289 views
2 answers 264 views
264 views asked Feb 27, 2020 by avibootz
...