How To Start With Node.js

Let’s start with the question, "What is NodeJS?"

Well, simply, Node.js is a server framework which runs on various platforms like Windows, Linux, Unix, Mac OS X. It is open source. 

According to NodeJS website, "Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world."

So, what is V8?

NodeJS

According to Google Developers

V8 is Google’s open source high-performance JavaScript engine, written in C++ and used in Google Chrome, the open source browser from Google, and in Node.js, among others. It implements ECMAScript as specified in ECMA-262, and runs on Windows 7 or later, macOS 10.5+, and Linux systems that use IA-32, ARM, or MIPS processors. V8 can run standalone, or can be embedded into any C++ application.

More information can be found on V8's public wiki.

How to start

We need to install & setup NodeJS development environment to work with. Go to NodeJS page to download the MSI file.

NodeJS

Click "Next" to complete the setup. As we planned to develop a sample application using Visual Studio, please make sure that the IDE has NodeJS development package installed.

NodeJS with Visual Studio

Open Visual Studio 2017. Go to File > New > Project

NodeJS

A New Project window will appear; from the left menu, click JavaScript. It will show a list of sample NodeJS applications. I just started with a Blank NodeJS Web Application.

NodeJS

The initial sample has server.js and package.json file.

Node.js Web Server

  1. 'use strict';  
  2. var http = require('http');  
  3. var port = process.env.PORT || 1337;  
  4.   
  5. http.createServer(function (req, res) {  
  6.     res.writeHead(200, { 'Content-Type''text/plain' });  
  7.     res.end('Hello World\n');  
  8. }).listen(port);  

In the above code snippet, we can see that the initial sample app has a core module (HTTP) which has "http.createServer" method to handle the request from user on port 1337 with response.

Package.json

  1. {  
  2.   "name""startup-nodejs",  
  3.   "version""0.0.0",  
  4.   "description""StartupNodejs",  
  5.   "main""server.js",  
  6.   "author": {  
  7.     "name""Shashangka"  
  8.   }  
  9. }  

Now, run the application. The server will respond with the below output in the browser.

NodeJS

Now, if we want to serve HTML pages with touting in terms of the user request, we need to go for different NodeJS Frameworks. In this post, we are using Express.js to develop sample web application which can serve HTML page.

Node.js Frameworks

Express.js, Geddy, Locomotive, Koa, Total.js, Hapi.js and more. 

Express.js Web Application

To create Express.js web application. First of all, we need to install Express.js package.

Install express.js using npm

$ npm install express --save

Install Express.js in Visual Studio

NodeJS

Package.json

  1. {  
  2.   "name""nodejs-web-app1",  
  3.   "version""0.0.0",  
  4.   "description""NodejsWebApp1",  
  5.   "main""server.js",  
  6.   "author": {  
  7.     "name""Shashangka"  
  8.   },  
  9.   "dependencies": {  
  10.     "express""^4.16.2"  
  11.   }  
  12. }  

As we can see, our package.json has a dependency now with package version. Now, we need to modify our server.js file to serve HTML page.

Index.html

Create an HTML page to response with the user request.

  1. <!DOCTYPE html>  
  2.   
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <title></title>  
  7. </head>  
  8. <body>  
  9.     <h3>Hello NodeJS</h3>  
  10. </body>  
  11. </html>  

Server.js

In the below code snippet, Express.js module is imported by using require() function.

  1. 'use strict';  
  2. //var http = require('http');  
  3. var express = require('express');  
  4. var app = express();  
  5. var port = process.env.PORT || 1337;  
  6.   
  7. //http.createServer(function (req, res) {  
  8. //    res.writeHead(200, { 'Content-Type': 'text/plain' });  
  9. //    res.end('Hello World\n');  
  10. //}).listen(port);  
  11.   
  12. app.get('/'function (req, res) {  
  13.     res.sendFile('index.html', { "root": __dirname });  
  14. });  
  15.   
  16. var server = app.listen(port, function () {  
  17.     console.log('Node server is running on port..' + port);  
  18. });  

The app object responsible for routing user requests(HTTP), rendering HTML views. The app.listen() function creates the Node web server by listening defined port.

Output

Now, run the application and it will show the below output.

NodeJS

Now, let’s add another page called About. We also need to modify our existing server.js file & add the below code snippet.

  1. app.get('/about'function (req, res) {  
  2.     res.sendFile('about.html', { "root": __dirname });  
  3. });  

Add a menu to index & About page.

  1. <ul>  
  2.     <li><a href="/">Home</a></li>  
  3.     <li><a href="/about">About</a></li>  
  4. </ul>  

Finally, run the application. As we can see from the below image, the request is executed by responding to the About page.

NodeJS


Similar Articles