Creating Your First API using Node and Express JS

API is an abbreviation for "Application Programming Interface." APIs are a simple way for organizations to extract and share data. APIs can be created with any programming language and server-side software.

This article introduces you to building an API using Node, which is JavaScript's back-end implementation, and Express, a minimal back-end Node framework.

Prerequisites

Before getting started, you will need to have Node JS installed on your computer. You can do this by visiting the official Node website and following the steps on the "downloads" page.

You can verify if the installation was complete using the following command:

node --version && npm --version

Here is what I have. Your versions may look slightly different.

Setup

First navigate to where you want your project to be located, then create a directory using the command below:

mkdir first-express-api && cd $_

Here, we are simply making a directory called first-express-api then navigating into it.

At the root of your first-express-api directory, run npm init

npm init

npm stands for node package manager. It will describe what packages we need to run the server, how to run the server from the cli, and store what version of our code we are developing.

You will be prompted to answer some basic questions, or leave them blank if you want by typing "Enter". Once this is complete, you should have a package.json file that looks like this:

{
 “name”: “first-express-api”,
 “version”: “1.0.0”,
 “description”: “this is my first node project”,
 “main”: “index.js”,
 “scripts”: {
 “test”: “echo \”Error: no test specified\” && exit 1"
 },
 “author”: “Joel Nyongesa”,
 “license”: “ISC”
}

Installing Express

Express is a simple Node.js router. It enables developers quickly create simple endpoints. There are numerous Node.js routers, but Express is the most popular, with extensive documentation and examples. Express is a Node.js web application framework that provides an extensive range of HTTP utility methods and middleware for quickly and easily creating robust APIs.

Check out the official documentation here

Let's install Express using npm:

npm i express

It is also highly recommended to install Nodemon as a dev dependency. It’s a simple package that automatically restarts your server when files change:

npm i nodemon

You can then replace “test”: “echo \”Error: no test specified\” && exit 1" with the snippet below in package.json:

"scripts": {
    "dev": "nodemon server.js"
  },

In the end, your package.json file should look like this:

{
  "name": "first-express-api",
  "version": "1.0.0",
  "description": "My first API made using express",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon server.js"
  },
  "author": "Joel Nyongesa",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "nodemon": "^3.0.2"
  }
}

Creating your first API

At the root of your project, create an index.js file as below:

Inside index.js, lets begin by importing express and creating our application:

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

Then specify a port and fire up our server as below:

const port = 8000;
app.listen(port, ()=> {
    console.log(`Server is listening on port ${port}`);
})

Your complete index.js file should look like this:

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


const port = 8000;
app.listen(port, ()=> {
    console.log(`Server is listening on port ${port}`);
})

Now start your server by running the command:

npm run dev`

This should print out the message "Server is listening on port 8000" in your terminal.

Interesting, ha?

Let's make it more intresting by creating our first endpoint

Your First Endpoint

To create your first endpoint, we will use the following snippet:

app.get('/', (req, res) => {
    res.send('Hello there, this is my first Node API')
});

We are simply using a GET method at the "/" endpoint to print out the message "Hello there, this is my first Node API".

Your complete index.js file should look like this:

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

// Defining our routes
app.get('/', (req, res) => {
    res.send('Hello there, this is my first Node API')
});

const port = 8000;
app.listen(port, ()=> {
    console.log(`Server is listening on port ${port}`);
})

Now open your browser or POSTMAN and head over to localhost:8000. You should see something like:

Other similar HTTP methods include POST, PUT, PATCH, DELETE.

Congratulations! You just made your first API using Node and Express!

Thank you for reading; please leave any questions, comments, or suggestions in the comments section. Your feedback is highly appreciated.

Happy coding!