How on NodeJS to determine the current URL of the page?

Of course there is an example of parsing a pre-entered URL , but the hunt is to count the URL that is in the address bar. If the topic has already been asked to specify.

I tried it myself:

var url = require("url"); //текущий URL? console.log( url.href + '\n' + url.protocol + '\n' + url.hostname + '\n' + url.port + '\n' + url.pathname + '\n' + url.search + '\n' + url.hash ); //вывод undefined.. 
  • Speaking of URLs in server context? Or am I confusing something? - Dmitriy Simushev
  • Well, if the PHP language is talking about the SERVER variable)) Roughly speaking, I launched the application at localhost: 8080 / myapp? row = 5 - this whole line should be considered - serg

1 answer 1

In your example, the concept of " Current URL " is meaningless.

Unlike PHP, where each file can handle requests to a separate URL in node.js there is only one centralized application that processes requests to all URLs. There are no "superglobal" variables containing "current URL" in node.js: it can not be : an application can process several requests at the same time.

If we draw analogies with the world of PHP, then this is the same principle used in Symfony: an application receives a request object and must return a response object. However, the URL is known only in the context of the request.

Based on the foregoing, a typical node.js application-HTTP server (without using third-party libraries) can be:

 var http = require('http'); var server = http.createServer(function(req, res) { // Вывод URL к которому было произведено обращение console.log(req.url); // Отдача ответа. res.write('output'); res.end(); }); server.listen(8000); 

As for the url module, its purpose is to provide you with the means to parse some previously known URL.