Let’s cover a bit of basics first.
What is NodeJS?
It is a Server-side, JavaScript platform built on Google Chrome’s JavaScript V8 engine. It is an open source and a cross platform application to develop server side and networking applications. Anyone can develop the Node.js application by writing a code in JavaScript and it can run on Microsoft Windows, Linux, or OS X.
Features
- Fast in code execution.
- Highly scalable
- Event driven and asynchronous
- No buffering
Download and installing NodeJS.
Visit the website and download the NodeJS exe.
Download the V6.2.2 version.
Install it by following the instructions, shown below:
After the installation is complete, search node in Windows search option and open it
Search for the current NodeJS version and npm version (just some information for your own sake) by using the commands, given below:
Now, in this command Window, type “node," the directory path will be removed and the cmd will start accepting node code, shown below:
You can now go ahead, do some calculations, and try and print the messages on the console, as shown below:
This shows that the node is successfully installed in your system and is working as expected.
Please press Ctrl+C twice to exit from the node and have a directory path again.
We will now create a small piece of code in a js file and will try to run it using NodeJS. Let’s see how it’s done.
Create a js file, say “testingnode.js” file and write the code, given below:.
-
- var http = require('http');
-
- http.createServer(function (req, res) {
-
- res.writeHead(200, {'Content-Type': 'text/plain'});
- res.end('Hello Everyone. From Vipul Malhotra');
-
- }).listen(8090);
-
- console.log('Server running port 8090');
Now, using cmd, go to the directory where this file is saved and run the command “node testingnode.js”.
Cmd will print the console msg that we have written in the file, shown above. Server is running at a port 8090.
Now, migrate to the URL http://localhost:8090/ to view the o/p 'Hello Everyone. From Vipul Malhotra' that we have written in the js file.
Please note that 8090 here is the port number that we have provided in our js file, shown above. You can provide any available port number to your application, but you would have to call the application using the port number.