Node.js Quickstart Guide

Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Things to Know Before Starting with Node.js

Web applications include both client and server-side models. Once the server gets a request from the client for a particular page, it responds with the appropriate resources. Thus, a server responds to millions of requests in a fraction of seconds as threads.

What is Node.js?

It is an open-source powerful JavaScript-based platform used by thousands of developers to develop I/O intensive web applications. It is a complete runtime environment for the back-end which responds to all the request of the clients. It is particularly suited to building applications that require some form of real-time interaction with the users, for example, chat sites, or apps such as Codeshare, where you can watch a document being edited live by someone else.

Introduction to Express

It is a small framework of Node.js which provides easy to use functionality for a web server. Open-source framework available to build a single, multi or hybrid page website. It facilitates the easy use of middleware and routing along with rendering of the HTTP requests.

Installation and Starting a New Project

Install Node.js for Windows from its official document and for Mac OS by the following command lines

nvm install v0.8.10
nvm use v.0.8.10

Start a Project by Initializing npm

Create a new directory name as new_project, then initialize npm considering it as the root directory. Then provide the basic license and description of the project to start with. This results in creating all the need files to start with the project which includes node modules, package-lock.json, and package.json.

mkdir new_project && cd new_project
npm init or yarn init

You can install express using the following command from npm:

npm install express –-save

The Server-Side

We’ll start with the classic “hello world” example, explain that, and then build from there.

First, create a new file called server.js that will hold the code for our Express framework. In this file, write the following code:

By requiring Node modules they aren’t automatically injected into the global scope, but instead, you just assigned them to a variable of your choice. That means that you don’t have to care about two or more modules that have functions with the same name.

Behind the scenes:

1. We use require function to include the express module to our project. Require in node.js is similar to import in python. Its function is to load particular dependencies,

2. Create an object app for the express module

3. In the third line we creating a route for the get request for the port http://localhost:3000. The callback function (arrow function js6) responds with ‘Hello World’.

4. req and res symbolize request and response respectively. Then the client will receive a word

‘Hello World’

5. We are then using the listen to function to make our server application listen to client requests on port no 3000. You can specify any available port over here

With server-side programming, we can execute different blocks of code based on the user’s route. A route is actually a path on the server like http://server.com/path_to_route. With static files, this is just a folder structure, but new possibilities open up when you programmatically handle a route. For example, the following code specifies a function to call whenever a user goes to http://server.com/showpage.

app.get(‘/showpage’, sayHello);

Path to different files:

You can also require relative files in case the project has a lot number of components and modules which can be done by the command:

As Example:

var myfile = require(‘./myfile’);

Middleware: Middleware is a function that obtains request(req) and triggers some action before responding(res) execute the following tasks along with providing a continuation function to call next()

Some of its uses are:

· Execute provided code

· Make changes to req and res or end the cycle

· Call the next middleware function in the stack

You can think of express’s middleware stack as an array of functions.

Example:

Run the project by:

After creating a project you can start the application in the local server. It should run continuously in your localhost in order to visit the site. Use any browser to open the localhost as:

http://localhost:3000/

In command line run the below commands after navigating to the root directory (name as given during initialization)

node app.js or index.js 

Happy Learning!

--

--