In this article I am going to show you how to setup Node.js, how you can use it after setup, usage of Node.js modules, NPM packages and Node.js HTTP Server.
Installation and Verification
Firstly, download the Node.js from the below link and install it like you do for any other software.
By default Node.js uses C:\Program Files\nodejs as the default installation path. At the time of installation Node.js will set this directory path in system environment variables. In case if you chose any other path, make sure it is properly registered in environment variables.
Once you install the Node.js, you must verify the installation to make sure Node.js installed as expected. You can verify the Node.js installation by using one of the following methods.
Method 1: search for Node.js from your windows search and find the Node.js file highlighted in below picture.
Select the Node.js file to launch its Node.js shell as shown below.
If you want to exit the Node.js, give Node command .exit and hit enter.
Method 2: You can also verify the Node.js installation from your regular command prompt by giving command node as shown below. Once you given node command, you will be switched to Node.js shell.
If you want to switch back to your regular command prompt, type .exit command and hit enter
Node.js has a module loading feature where one JS file can be loaded into another JS file. Which is a very useful feature where if you have a dependent files.
Below is the output when you run the above “sample.js” file in Node.js.
Now let’s make the “module1.js” little bigger with couple of methods to call them from “sample.js” as shown below:
That’s it about loading modules. Isn’t it so simple? Yes, of course.
Node Package Manager (npm) is used to install the third party modules/plugins. You can search for NPM packages from npmjs.
Installing/Creating the NPM packages requires the package.json file at the root directory of “sample.js” file to maintain the dependent files. User the npm init command to create the package.json file as shown below.
Once the package.json file is created, we can install the package using npm install command. Here I’m installing the datetime package and using the same in “sample.js” file.
Now let’s go and modify our “sample.js” file to make use of datetime module as shown below.
In the above picture you can see we use the datetime module in “sample.js” and called the formatAgo() method of datetime. Here is the output of “sample.js” after using the npm package.
Node.js HTTP Server
This title will confuse you. Node.js is not a webserver like any other IIS, Apache, etc. But within Node.js you can create a HTTP Server which can listen the HTTP request on a specific port. Here I am going to create a Node.js HTTP Server which listens the requests on port number 8888.
Node.js comes with default http module which is used to create the server and listen http requests. When you run the above “httpsample.js” in Node.js, http server will create inside Node.js runtime and start listening the requests as shown below.
Whenever you make a request to server on port 8888, Node.js start responding and take control over the response. You can test this request by making a call to http://localhost:8888/
When you stop the Node.js http server, your http://localhost:8888/ URL no more valid and doesn’t respond for your request.
That’s all for now. Hopefully this article gives you more idea about Node.js and how to use it for your development.