ExpressJS Series: Setting up a Basic ExpressJS Server

Ganesh B
3 min readOct 1, 2018

--

Creating a ExpressJS server needs a basic understanding of npm (node package manager) and how it works. npm helps you manage NodeJS application package dependencies, period. For those who donot know npm, follow and understand the npm commands.

Let us get started with creating a basic ExpressJS server. Let us first create a project folder for NodeJS applications. In all my future blogs, I will be using Ubuntu commands (debian based linux distro), but most commands I present will work on any OS. If there is a OS specific command needed, I will try to mention that specifically. But if I miss do point it out, I will add the same. Else, just follow along.

I am using a folder named projects to create all my ExpressJS projects. Use your own folder location that you are comfortable with for development. Let us create a folder called as basicsrv inside projects and move/traverse into it to create our first ExpressJS application.

cd basicsrv

In the folder, initiate a npm project to create a package.json.

npm init

Follow the npm project initialiser questions to finally initialize the project using package.json. After all the steps/questions are done and you confirm details with a yes, the command follows with a creation of a package.json in the folder basicsrv. Once the package.json is created, install the ExpressJS framework package in the project.

npm install expressjs --save

This process will install ExpressJS in a node_modules folder. Create a file name as index.js inside the basicsrv folder.

Lets put following contents into it:

Let us understand the above code:

Line 1: Gets the Express Framework into the file module index.js.

Note: Every file, e.g: index.js, is a file module, and is isolated from other files. If you want to use an object from a different file inside index.js, you have to export the object using export command and import the object inside index.js using require command; respectively. ExpressJS has exported the framework object from its project (the one we installed inside the project folder basicsrv in node_modules). Hence, we are able to require it inside index.js.

Line 2: We are instantiating the framework object

Line 3: We are telling the ExpressJS framework object that if someone pings or makes a request to the server at the path / then send a response with status 200 ok and body with {status: “running”, time: “whatever the current time”}

Line 4: We are defining which host the server will be running.

Line 5: We are defining which host the server will be listening to.

Line 5: We now start the server to list at respective host and port we defined. Once the server has started, it will run the 3rd argument (a callback function) which is called after the server has started. It will log out to the console server started.

We have now written a simple NodeJS server based on ExpressJS framework. Let us start it. In the command line (inside the folder path basicsrv) type the following:

node index.js

This will run the index.js file, and start the server which is listening on 127.0.0.1:9001. Open the browser and browse to https://127.0.0.1:9001. You will get the JSON object response that looks something like this:

{status: “running”, time: “zzz”}

Your basic ExpressJS server is set up.

In the next blog, we will create different routes or url paths with different HTTP methods like GET, PUT, PATCH, POST, DELETE.

Creating Routes for ExpressJS Server: https://medium.com/@ganeshsurfs/expressjs-creating-routes-for-expressjs-server-27e38316c015

ExpressJS Series All Blogs: https://medium.com/@ganeshsurfs/expressjs-series-links-9e038be8d78b

Let me know how I did, and if you learnt something new. Do leave your comments, and dont forget to like the article.

--

--