How to set up a Node.js Server

Ajinkya
The StickyBit
Published in
4 min readMay 16, 2020

An overview of what node.js is, how it works and a step by step guide for setting up a node.js server.

This blog will cover steps for setting up a Node.js development environment. We will also walk you through some of the basic concepts of Node.js. And here we begin!

First things first, let’s check our basic knowledge!

Node.js is a server-side back-end framework based on JavaScript. But you know what, Node.js is written using below three languages-
C, C++ and JavaScript. Interesting!

Node.js internally uses V-8 JavaScript engine built by google which compiles the JavaScript code to native machine code at runtime. So, Node.js is also called a language engine.
npm (Node package manager) — It is a default package manager used by Node.js and is developed in JavaScript. It is basically a collection of JavaScript libraries. You can install the libraries you need by using the following command-

npm install package_name --save

save flag is used to install a library as a development dependency and is optional.

How does Node.js work?

Node.js is a single-threaded, non-blocking IO system. It uses an event-loop mechanism to handle all incoming API requests. This allows it to support 10's of 1000's of concurrent connections.

Event loop mechanism

Let’s quickly set up a demo project!

Step 1- Install Node.js on your machine. Already installed? Jump to Step 2.
Not installed? Here’s the link for downloading.
Download Node.js specific to your operating system. Complete the installation process as directed in the installation wizard.

Step 2- After successful installation (hopefully), create a folder named ‘node-demo’, open it and fire the following command.

npm init

This is a project initialization command and will walk you through creating a package.json file. Enter the details you have been asked for. If you find it boring, press enter to skip adding details. Here’s an image for your reference.

In the end, it will show you a preview of the package.json file getting created. Type yes and you are done with the project initialization step.

Step 3- Lets now install express and body-parser libraries.

What is Express? It is Node.js based web application framework that provides a set of features required for a web-based application.

What is Body parser? It helps us to read data coming from HTTP post requests. It is a part of express middle-ware, which reads form data and stores it in a JavaScript object format which makes it accessible through the request body.

npm install express body-parser --save

Step 4- Creating an express server.
Create an index.js file in the root folder and place the following code in the file.

Now, open package.json file and put following script command in the “scripts” object

“start”: “node index.js”

Now open the terminal, jump into the project we created and run the following command.

npm run start

What did we just do?
We created a command “start” which internally runs “node index.js”. You can also start the server with “node index.js”.

This will start our node server on port: 3000

Step 5 (Final step)- Open your browser and hit URL http://localhost:3000/ and what do you see? A response sent from the server, yaay!

Browser Window

This is how we set up a Node.js server. If you are eager to know more about it, please visit Node.js official website.

Companies using Node.js

Some of the companies using Node.js

If you face any problems in setting up a server, let us know in comments and we will try our best to solve your queries. Stay tuned for the next blog. Till then, keep learning!

--

--

Ajinkya
The StickyBit

Javascript Enthusiast, Fullstack javascript developer