How to Make a Simple API Using ‘express’ in Node.js

Souvik Paul
Oct 5 · 3 min read

Hello world, if you are a newbie in Node.js, then you must be tempted for making your own server in your own local machine. So, if you are in this blog then you are at the right place. So let’s dive in.

Image for post
Image for post
Photo by Shahadat Rahman on Unsplash

You can also find this project in my GitHub repository-

First of all we’ll open the terminal and make one directory for our server application. Then we’ll navigate into that directory and run the command — npm init. Then you will be asked to enter various details about your application, which will be stored as a json file named ‘Package.json’. Then you have to run another command to install the express module in that directory — npm install express --save.

According toexpressjs.com, is a fast, unopinionated, minimalist web framework for node.

Now, we’ll create our server application file with the name of ‘app.js’ in the same directory. Then we’ll open this file in any text editor like Visual studio code,Notepad, Notepad++, Sublime Text 3 etc and write the following codes -

In the above code, var express = require("express"); will import the express module into our application. Then, var app = express(); will create an instance of express, named ‘app’. The third and the fourth line which contains app.get() will respond to the GET request made by the browser. When the user will type http://localhost:3000/in the browser, then the following code will be executed-

When the user will type http://localhost:3000/one in the browser, then the following code will be executed-

Then, we need to make our server application listen to all the requests made by the browser, which will be achieved by the following command-

In this application, we have chosen port 3000, on which our server will listen to all the requests. You can choose any port of your choice.

: Now that we have written all the codes for our server application, we’ll save it and go back to terminal to run this using the command node app.js. The following message will be displayed in the console -

Server listening on port: 3000

So, now our server is up and running.

We’ll go the browser and enter the url http://localhost:3000/ . The browser will display the following string —

Hi there, thanks for checking out my work. The server that is responding to you is developed using 'express.js' module in node.js. Please type 'localhost:3000/one' in your URL search bar to view the next page.

Now, we’ll enter another URL http://localhost:3000/one . The browser will display the following string -

Have a nice day

In this article, we have discussed about the Express.js, we have created two API endpoints, which will accessed from the browser using GET request and finally we’ve made our server application to listen on port 3000.

https://www.npmjs.com/package/express

A complete documentation for using express module.

The Startup

Medium's largest active publication, followed by +733K people. Follow to join our community.

Souvik Paul

Written by

You can also follow me on LinkedIn -https://www.linkedin.com/in/souvikpaul-plus/

The Startup

Medium's largest active publication, followed by +733K people. Follow to join our community.

Souvik Paul

Written by

You can also follow me on LinkedIn -https://www.linkedin.com/in/souvikpaul-plus/

The Startup

Medium's largest active publication, followed by +733K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store