J
J
JimmyTheWorm2018-10-16 16:24:43
Node.js
JimmyTheWorm, 2018-10-16 16:24:43

What module to use for parsing url in node.js?

Hello. Tell me please. I have a site on node js + express. I want to track where the user comes from, and change the content depending on the source. Faced a sooo banal problem, already embarrassing - parsing url parameters. And I can not find a suitable module. Of course, it would be possible to parse it manually, but I want something more refined)
As a result, the transition to the url like mysite.com/?rce=google&utm_medium=cpc&ref_id=1
must be turned into an object:
{rce:"google",utm_medium:"cpc" ,ref_id:1}. There are a lot of parameters, I won’t list them all, well, I think you understand the essence.
everything that comes after the host is received through req.originalUrl (correct if there is a better way), as a result it turns out like this: /?rce=google&utm_medium=cpc&ref_id=1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eternalfire, 2018-10-16
@JimmyTheWorm

Use req.url builtinurl

var url = require('url');
var url_parts = url.parse(request.url, true);
var query = url_parts.query;

Also, express already has req.query
var id = req.query.id; // $_GET["id"]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question