AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Node.js URL Module


<< Previous Page
Nodejs tutorial
Next Page >>
Nodejs tutorial

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

new URL(input[, base])

The ‘url’ module can be accessed using:

const url = require('url');

 

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:

Example: Split a web address into readable parts::
var url = require('url');
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:

demo_test_fileserver.js:
var http = require('http');
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

Example: Create a js file named my_test_main.js with the following code −:
// Using require to access url module
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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


CSS background color - opacity, gradient...
CSS flex - display: flex - CSS Flexbox L...
CSS border padding...
HTML text center horizontally and vertic...
HTML text input <input type="text"&g...
HTML link color...
How to Link CSS to HTML, external file n...
HTML link - hyperlink...
jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...