Node.js URL Module
Node.js Tutorial » Node.js URL Module
What is url module in node JS??
The URL module method is an inbuilt application programming interface that divides a web address into readable parts.
To include the URL module, use the require() method:
Syntax
The ‘url’ module can be accessed using:
Node.js url.parse() method
Parse an URL address with the url.parse() method, and it will return a URL object with each part of the address as properties:
var adr = 'https://www.agernic.com/nodejs/global-object.html?year=2021&month=april';
var q = url.parse(adr, true);
console.log(q.host); //returns 'www.agernic.com'
console.log(q.pathname); //returns '/nodejs/global-object.html'
console.log(q.search); //returns 'year=2021&month=april'
var qdata = q.query; //returns an object: { year: 2021, month: 'april' }
console.log(qdata.month); //returns 'april'
Output:
C:\domains\My_Name>node demo_test_url.js
www.agernic.com
/nodejs/global-object.html
year=2021&month=april
april
Related subjects:
node.js http module
node.js modules
global objects nodejs
Tags: nodejs url module exemple, link module, install urk module, valid-url nodejs module
What is url module in node JS?
What is a url module?
How do I declare a url in node JS?
How use url parse in node JS?
Node.js File Server
Now we know how to parce the query string and how to make Node.js behave like a file server.
Let's combine the two and serve the file requested by the client.
We create two html files as: spring.html, the autumn.tml and save them in the same folder as the node.js files.
Create a Node.js file that opens the requested file and returns the content to the client. If anything goes wrong, throw a 404 error:
var url = require('url');
var fs = require('fs');
http.createServer(function (req, res) {
var q = url.parse(req.url, true);
var filename = "." + q.pathname;
fs.readFile(filename, function(err, data) {
if (err) {
res.writeHead(404, {'Content-Type': 'text/html'});
return res.end("404 Not Found");
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
Initiate demo_test_fileserver.js: C:\Users\Your Name>node demo_test_fileserver.js
Node.js demonstrate the new URL() method
const url = require('url');
const newUrl = new URL(
'https://agernic.com/r/o/u/t/e?query=string#hash');
// url array in JSON Format
console.log(newUrl);
const myUR = url.parse(
'https://agernic.com/:3000/r/o/u/t/e?query=string#hash');
console.log(myUR);
console.log(URL === require('url').URL);
const myURL1 = new URL(
{ toString: () => 'https://agernic.com/' });
console.log(myURL1.href)
nodejs url module exemple, link module, install urk module, valid-url nodejs module
What is url module in node JS?
What is a url module?
How do I declare a url in node JS?
How use url parse in node JS?
Node.js URL Module - nodejs
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.