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

Syntax:

  • 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

Example:

  • 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(q.host); //returns 'localhost:8080'
console.log(q.pathname); //returns '/default.htm'
console.log(q.search); //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.


Wikitechy.html

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

Output:

    Wikitechy

    Wikitechy is an enhanced tutorials

    Wikitechy Technology.html

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

    Output:

    Wikitechy Technology

    Wikitechy.com 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:

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