How to Create A Simple API (ExpressJS

An API stands for Application Programming Interface. It is a set of rules that allows two applications to communicate with each other. In this article, we will be discussing how to create a simple API using ExpressJS, a popular JavaScript framework.

ExpressJS is a lightweight framework that provides a wide range of features for web and mobile applications. It is easy to use and provides a great deal of flexibility. In addition, it has a large community of users and developers who are always willing to help.

The first step is to install ExpressJS. You can do this using the Node Package Manager (NPM). If you don't have NPM installed, you can get it from here. Once you have NPM installed, open a terminal window and type the following command:

This will install ExpressJS and all of its dependencies.

npm install --save express

Next, you need to create a file called app.js in our project's root directory. This file will contain the code for our API.

Now that you have your project set up, you can start coding your API. First, let's require the ExpressJS module.

var express = require('express');

Next, you need to create an instance of the ExpressJS application.

var app = express();

Now that you have an ExpressJS application, you can start adding routes. Routes are URL paths that are mapped to specific actions. For the API, you are going to create two routes. The first route will be used to retrieve a list of users and the second route will be used to create a new user.

To create a route, use the app.get() or app.post() methods. These methods take two arguments. The first argument is the path, which is the URL that the route will be mapped to. The second argument is a callback function that will be executed when the route is accessed.

app.get('/users', function(req, res) { // Code to get a list of users }); app.post('/users', function(req, res) { // Code to create a new user });

Now that you have your routes defined, you need to write the code inside of callback functions to actually do something. For the /users route, you are going to return a list of users in JSON format.

To do this, first require the fs module, which will give access to the file system.

var fs = require('fs');

Next, read the users.json file and parse it into a JavaScript object.

var users = JSON.parse(fs.readFileSync('users.json', 'utf8'));

Finally,to send the list of users back to the client set the Content-Type header to application/json and calling the res.send() method.

res.setHeader('Content-Type', 'application/json'); 
res.send(users);

The code for /users route should look like this:

app.get('/users', function(req, res) 
{ var fs = require('fs');
 var users = JSON.parse(fs.readFileSync('users.json', 'utf8')); 
res.setHeader('Content-Type', 'application/json'); 
res.send(users); });

Now move on to /users/:id route. This route will be used to retrieve a single user by their ID. Use the req.params object to get the ID from the URL. This object contains all of the URL parameters.

app.get('/users/:id', 
function(req, res) { var id = req.params.id; // Code to get a user by their ID });

Now that you have the ID, use it to find the user in users list.

var user = users.find(function(user) { return user.id === id; });

If the user is found, send back their data in JSON format. If the user is not found, return a 404 status code.

if (user) { res.setHeader('Content-Type', 'application/json'); 
res.send(user); } else { res.status(404).send(); }

The code for our /users/:id route should look like this:

app.get('/users/:id',
 function(req, res) { var id = req.params.id; 
var user = users.find(function(user) { return user.id === id; }); 
if (user) { res.setHeader('Content-Type', 'application/json'); 
res.send(user); 
} else {
 res.status(404).send(); } });

Now that you have /users route complete, move on to /users route. This route will be used to create a new user.

Use the req.body object to get the data for the new user. This object contains the data that was sent in the request body.

app.post('/users', 
function(req, res) 
{ var user = req.body; // Code to create a new user });

Now that you have the data for the new user, you need to add them to the list of users.

users.push(user);

Finally, write the updated list of users back to the users.json file.

fs.writeFileSync('users.json', JSON.stringify(users));

The code for your /users route should look like this:

app.post('/users', function(req, res) { var user = req.body;
 users.push(user); 
fs.writeFileSync('users.json', JSON.stringify(users)); 
res.status(201).send(); });

Now that you have your API complete, Start your ExpressJS server. Do this by calling the app.listen() method.

app.listen(3000, function() { console.log('Server listening on port 3000'); });

This will start the server on port 3000. You can now access your API at localhost:3000/users.

Congratulations, you have just created a simple API using ExpressJS!