Dani
Dani

Reputation: 15081

use express.js to get the query string of a url

I'm looking for a way to get the query string part of a url using node.js or one of it's extension module. I've tried using url and express but they give me an array of parameters. I want to original string. Any ideas ?

example; for:

http://www.mydom.com?a=337&b=33

give me

a=337&b=33

(with or without the ?)

Upvotes: 0

Views: 276

Answers (2)

Jordan Running
Jordan Running

Reputation: 106147

Use url.parse. By default it will return an object whose query property is the query string. (You can pass true as the second argument if you want query to be an object instead, but since you want a string the default is what you want.)

var url = require('url');

var urlToParse = 'http://www.mydom.com/?a=337&b=33';
var urlObj = url.parse(urlToParse);

console.log(urlObj.query);
// => a=337&b=33

Upvotes: 4

Touffy
Touffy

Reputation: 6561

How about using the built-in url.parse method?

Upvotes: 1

Related Questions