A beginner’s attempt to explain how to build a simple Rest API with Nodejs and Express.

Temiloluwa Abiodun-Ojo
devcareers
Published in
3 min readSep 12, 2019

API means Application Programming Interface and in simple terms, what it does is that it allows applications to communicate with one another. An API is the messenger that takes a request, tells a system what you want to do and then returns the response back to you! So what then is a Restful API? REST is short for Representational State transfer and it is one which when called, the server transfers a representation of the state of the requested resource to the client.

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. You can click here for more information on Node. Express.js, or simply Express, is a web application framework for Node.js. It is designed for building web applications and APIs. It has been called the most commonly accepted server framework for Node.js.

Please read through the documentation of each of the above packages for how to install and walk with me as we build a simple rest API using them.

First and foremost, create a folder can call it any name you want. Next, we need to create a package.json file inside of that folder using the npm init - -y command. Next, create an index.js file too in the same folder and add the following code to it.

const express = require('express');const app = express();const port = process.env.PORT || 3000;app.listen(port,()=>{   console.log('listening on port ' + port);})

In the above code, we are basically just importing express into our project and assigning the port we want the server to listen on a variable called port and then calling the listen method on our variable app which is an instance of express.

Next, we create a JSON file which contains JSON data that we’ll use to test our API endpoints. Add the contents below to your JSON file and name it videodata.json

[{"categoryID": "294","parentID": "304","subjectID": "7","categoryName": "Apps and Side Dishes (Laura)","categoryDescription": "Learn to make amazing appetizers and side dishes with Laura in the Kitchen.","videosCount": "101","forumCategoryID": "163"},{"categoryID": "285","parentID": "304","subjectID": "7","categoryName": "Side Dishes","categoryDescription": "Side dish recipes for salads, vegetables, sauces with Hilah cooking.","videosCount": "38","forumCategoryID": "163"},{"categoryID": "337","parentID": "304","subjectID": "7","categoryName": "Side Dishes (bt)","categoryDescription": "Side dish recipes with Byron Talbott.","videosCount": "5","forumCategoryID": "163"},{"categoryID": "301","parentID": "304","subjectID": "7","categoryName": "Side Dishes for Barbecue","categoryDescription": "Barbecue side dish recipes done on the grill by the BBQ Pit Boys!","videosCount": "43","forumCategoryID": "163"},{"categoryID": "297","parentID": "304","subjectID": "7","categoryName": "Soups and Salads (Laura)","categoryDescription": "Looking for the perfect recipe to start your meal? Or are you looking to eat something on the lighter side? These are sure to have you covered!","videosCount": "70","forumCategoryID": "163"}]

Next, we have to require the videodata.json file in our index.js file so that our API endpoints can make use of it.

app.use(express.json());const data = require('./videodata.json');//.= app.locals.videodata;app.get('/api/videos', (req,res) => {res.send(data);})app.get('/api/videos/:categoryID', (req,res) => {const category = data.find((a) => a.categoryID == req.params.categoryID);if(!category){return res.status(404).send('Category does not exist');}return res.status(200).send(category);})app.post('/api/videos', (req,res) => {const newInfo = {"categoryID": req.body.categoryID,"parentID": req.body.parentID,"subjectID": req.body.subjectID,"categoryName": req.body.categoryName,"categoryDescription": req.body.categoryDescription,"videosCount": req.body.videosCount,"forumCategoryID": req.body.forumCategoryID}data.push(newInfo);res.send(newInfo);})

Type the above code just above the app.listen.

The URLs in the screenshot are called endpoints. The get and post just before them are called HTTP verbs. With the first get method, i.e

app.get(‘/api/videos’, (req,res) => {

res.send(data);

}), we are trying to retrieve all data in videodata.json while for the second get method, we are trying to retrieve a single item based on a particular parameter which in this case is the id. Lastly, with the post method, we are attempting to add new data to the array of objects.

Every one of these endpoints can be tested using POSTMAN.

I hope this helps someone just starting out with Nodejs and Express.

--

--