Most Popular Tutorials
Most Popular Tutorials :-

Simply Easy Learning at Your Fingertips. Click Tutorials Menu to view More Tutorial List





What is URL module in node.js?

How to use URL module in node.js

Url module is the core modules of node.js, which is used to parse the URL and its other properties.

The URL module splits up a web address into readable parts. To include the URL module, use the require() method:

an HTTP server will require information from the request URL to accurately process a request. This request URL is located on the url property contained within the req object itself.

var url = require('url');
Parse an address with the url.parse() method, and it will return a URL object with each part of the address as properties:

Example

File Name : example.js

var url = require('url');
var adr = 'http://localhost:8080/default.htm?year=2022&month=April';
var q = url.parse(adr, true);

console.log(q.host); //returns 'localhost:8080'
console.log(q.pathname); //returns '/default.htm'
console.log(q.search); //returns '?year=2022&month=april'

var qdata = q.query; //returns an object: { year: 2022, month: 'April' }
console.log(qdata.month); //returns 'April'

Example :-

File Name : url_test.js

var http = require('http');
var url = require('url');

http.createServer(function (req, res) {

var queryString = url.parse(req.url, true);
console.log(queryString);
// console.log("Complete href is :-"+queryString.href);

}).listen(8080);

Example

File Name :

var http = require('http');
var url = require('url');

http.createServer(function (req, res) {

var queryString = url.parse(req.url,true);
queryString.href = "www.ittutorial.in/xyz.html";
console.log("Complete href is :-"+queryString.href);

}).listen(8080);





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here