Creating a basic rest API with express

Node.js is an open-source, cross-platform runtime environment that allows developers to create all kinds of server-side tools and applications in javascript. It was designed to optimize throughput and scalability in web applications and is a good solution for many common web-development problems (e.g. real-time web applications). Express is the most popular node web framework, and is the underlying library for a number of other popular node web frameworks. It provides mechanisms to write handlers for requests with different HTTP verbs at different URL paths (routes).

In this article, let’s learn how to create a basic get request using express.js:

Prerequisites:

1. Installation of node of npm.

2. Basic knowledge of javascript.

If you need help with the installation of node and npm, you can check this post right here.

Getting started:

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Let’s use express to start creating our first get request. Assuming you’ve already installed node.js, create a directory to hold your application, and make that your working directory.

mkdir <app-name>cd <app-name>

Once you are inside the working directory, let’s initialize the project by using the “npm init” command. This command is a step-by-step tool to scaffold out your project.

npm init

It will prompt you for input for a few aspects of the project in the order below and you can simply hit enter to accept the suggestion and move on to the next prompt. Once you run through the “npm init” steps above, a “package.json” file will be generated and placed in the current directory.

Once we have initialised the project, we can start installing the modules. Installing modules from npm is one of the most basic things you should learn to do when getting started with npm. The first dependency we are going to install is the express module. It is a perfect choice for a server when it comes to creating and exposing APIs (e.g. REST API) to communicate as a client with your server application. To install express, we can run the following command:

npm install --save express

Running the above command will install express module into “node_modules” in the current directory. Whenever you install a module from npm, it will be installed into the “node_modules” folder. Once we have the required dependencies, let’s create a file named “index.js” in the current directory and open it in the code editor of your preferred choice. Inside the index.js, let’s import express as a module and invoke it:

const express = require('express');const app = express();

You can import any module that we installed into the code using node’s require function. We invoke the require function to import express and then assign the invoked function to another variable named “app” which will be useful to access inbuilt methods and properties, one such is for routing HTTP requests. To define routing:

app.get("/", function(req, res) {  res.send("Hello World!");});

The “app.get” method specifies a callback function that will be invoked whenever there is a GET request with a path “/”. This callback function takes a request “req” and a response “res” object as arguments and sends the response as “Hello World!”. The users makes a request to the server on the route “/” and receives response from the server as shown below.

Finally, we need to bind and listen our app on a specified host and port. We do this using:

app.listen(5000, function() {  console.log("App is up and running on port 5000!");});

Our app is now running on 5000 and it prints an acknowledgment message to the console. To get the server running, run the following command in the command line interface:

node index.js

This command will trigger the server using node’s inbuilt HTTP module. Once you run the following command, your interface should look similar to the one below:

You can see the output response from the server by loading “http://localhost:5000/" in your browser and it should display the following response

You have successfully created your first API get request with express 🚀👏🏻. You can get access to the source code by clicking here. Now that you have the basics of express, you can expand what you have like creating other HTTP methods, linking databases etc., Check out my other posts on node here.

Like this article? Follow Maneesha Venigalla.

Machine learning, AWS and full stack development.

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