Node.js URL Module

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:

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:


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

Now we know how to parse the query string, and in the previous chapter we learned how to make 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>I love the sun!</p>


<!DOCTYPE html>
<p>I love the snow!</p>

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();

Remember to initiate the file:

Initiate demo_fileserver.js:

C:UsersYour Name>node demo_fileserver.js

If you have followed the same steps on your computer, you should see two different results when opening these two addresses:

Will produce this result:

I love the sun!

Will produce this result:


I love the snow!