Home > database >  How to get the full URL in Node.js HTTP module?
How to get the full URL in Node.js HTTP module?

Time:08-21

According to How to get the full URL in Express

Let's say I have a URL:

http://localhost:3000/sample/page

import http from 'http';

const server = http.createServer((req, res) => {

            let url = req.url // /sample/page
              
    
}).listen(3000)

req.url will be /sample/page in this case

Is there any way to find out the full URL with host, port etc?

CodePudding user response:

To get the full URL, you need to get the protocol, host and originUrl

  1. protocol is http or https

  2. host is the website domain.

  3. originUrl is path

import http from 'http';

const server = http.createServer((req, res) => {

            let url = req.protocol   '://'   req.get('host')   req.originalUrl;
              
    
}).listen(3000)

var fullUrl = 

CodePudding user response:

You can find your answer here. This is exactly what you are looking for.

  • Related