Node has a number of built-in modules, ranging from filesystem access in a module called fs to utility functions in a built-in module called util.
A common task when building web applications with Node is parsing the URL.
When a browser sends a request to your server, it will ask for a specific URL, such as the home page or the about page. These URLs come in as strings, but we’ll often want to parse them to get more information about them. Node has a built-in URL parser module; let’s use it to see how to require packages.
Here in this example, we have to include URL module of Node. Node has several built-in modules, but some it may not be enough, so we have to use third party module. If we want to use the third party package then we have to define it in the package.json as a dependency.
Syntax for install third party package as npm
npm install thirdPartyPackageName –save.
Syntax for creating new project
Creating our own node module
Step 1: For creating our own custom module we are creating a function that will generate the random number between 0 to 100 and name this function as random-integer.js
NOTE: module.exports can be anything you want. Anything to which
you can assign a variable can be assigned to module.exports. It’s a function
in this example, but it’s often an object. It could even be a string or a number
or an array if you’d like.
Step2: To use the randomInteger function in another file, we have to import our randomInteger() module first, before we can use in another file.
Node Asynchronous World
Node is asynchronous in nature, If we had requested for the large external file to load from hard disk to server, which is external resources and when the second request come to the Node. We don’t have to wait for the first request to finished. We can start our second request. The example here for reading a file from disk, while reading for the external file we can proceed with next code, as Node asynchronous nature.
Building Node web server in Node
The HTTP module that makes it possible to develop web servers with Node, and it’s what Express is built on.The http.create-Server function, this function takes a callback that’s called every time a request comes into your server, and it returns a server object. The following listing contains a very simple server that sends “Hello world” with every request.
We can easily parse the request URL and we will show for dummy as