node js - Node js url module - node - node js tutorial - webnode

Related nodejs article tags - node js - node js tutorial - node js examples

What is an URL Module ?

  • This module contains functions to parse urls.
  • The functions of this module usually create an url object which contain properties which hold different parts of the url.
 nodejs URL image

Learn Node js - node js Tutorial - Node.js URL image - node - Node js Examples

The Built-in URL Module:

  • The URL module splits up a web address into readable parts.
  • To include the URL module, use the require() method:
  • Parse an address with the url.parse() method, and it will return a URL object with each part of the address as properties.
Related nodejs article tags - node js - node js tutorial - node js examples


  • The syntax for including the url module in your application:
Var url = require('url');

URL Methods:

Method Description
url.format() Returns a formatted URL string
url.parse() Returns a URL object
url.resolve() Resolves a URL


  • Split a web address into readable parts:
var url = require('url');
var adr = 'http://localhost:8080/default.htm?year=2017&month=february';
var q = url.parse(adr, true);

console.log(; //returns 'localhost:8080'
console.log(q.pathname); //returns '/default.htm'
console.log(; //returns '?year=2017&month=february'

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

Node.js File Server:

  • Node.js behave as a file server. Let us combine the two, and serve the file requested by the client.

Create two html files and save them in the same folder as your node.js files.


<!DOCTYPE html>
<p>Wikitechy is an enhanced tutorials</p>



    Wikitechy is an enhanced tutorials

    Wikitechy Technology.html

    <!DOCTYPE html>
    <h1>Wikitechy Technology</h1>
    <p> which is dedicated to teach you an interactive, responsive and more examples programs</p>


    Wikitechy Technology which is dedicated to teach you an interactive, responsive and more examples programs.
    • 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 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'});
        return res.end();
    Related nodejs article tags - node js - node js tutorial - node js examples

    Initiate demo_fileserver.js:

    C:\Users\Your Name>node demo_fileserver.js

    This wikitechy technological portal provides you whole lot of information related to the topics such as mongodb tutorial , what is node js , express js tutorial , node js tutorial pdf , learn node js , node js tutorial for beginners , node js tutorial w3schools , node js express , javascript for beginners , node js express tutorial , node js tutorial point , node js book , node js server , note js , node js mongodb , what is node js used for , why node js , node tutorial , node js basics , tutorialspoint node js , node express , node js sample application , nodeschool , node js for beginners , express tutorial , node js application , use of node js , npm tutorial , node js org , node js training , node js online training , node js tutorial beginner , how to use node js , node js hello world , express node js , node js mongodb tutorial , nodejs http , why use node js

    Related Searches to Node.js - URL Module

    Adblocker detected! Please consider reading this notice.

    We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

    We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

    We need money to operate the site, and almost all of it comes from our online advertising.

    Please add to your ad blocking whitelist or disable your adblocking software.