Backend Api
Backend Api
webdev
javascript
programming
tutorial
Node JS is a runtime environment that allows you to run JavaScript code outside of a web browser.
Express JS is a web framework that provides a set of features and tools for building web applications
with Node JS.
To install Node JS, you can download it from the official website and follow the instructions for your
operating system. To install Express JS, you can use the Node Package Manager (npm), which is a tool
that comes with Node JS and allows you to install and manage various packages and modules for your
project. To install Express JS, you can run the following command in your terminal:
npm install express
This will create a folder called node_modules in your project directory and install Express JS and its
dependencies there.
To create a project folder, you can use any name you like, such as express-api. To create a server file,
you can use any name you like, such as app.js. This file will contain the code for your backend API. To
create the file, you can use any text editor or IDE you prefer, such as Visual Studio Code, Sublime Text,
or Atom.
To start the server file, you need to import the express module and create an instance of the express
application. You also need to specify a port number for your server to listen to. You can use any port
number you like, as long as it is not already in use by another application. For example, you can use port
5000. To do this, you can write the following code in your server file:
const express=require('express');
const app=express();
const port=5000;
app.listen(port, () => {
});
To run the server, you can use the node command in your terminal, followed by the name of your server
file. For example:
node app.js
This will start the server and print a message in the console. You can stop the server by pressing Ctrl+C
in your terminal.
To create a backend API, you need to have some data to work with. For this example, we will use a
simple data model for a blog post, which has the following properties:
author: The name of the author of the post To store the data, we will use a simple array of objects,
which will act as a mock database. You can define the data model and the data array in your server file,
as follows:
return {
id: id,
title: title,
content: content,
author: author,
};
const posts = [
createPost(2, 'Express JS', 'This is a blog post about Express JS', 'Bob'),
createPost(3, 'RESTful API', 'This is a blog post about RESTful API', 'Charlie'),
];
To create the routes and the handlers for the API, you need to use the app object that you created
earlier. The app object has methods that correspond to the HTTP methods, such as app.get, app.post,
app.put, and app.delete. These methods take two arguments: a path and a callback function. The path is
a string that defines the URL for the route, and the callback function is a function that handles the
request and sends the response. The callback function has two parameters: req and res. The req object
represents the incoming request, and the res object represents the outgoing response.
To create the routes and the handlers for the API, you can write the following code in your server file:
res.status(200).json(posts);
});
const id = req.params.id;
res.json(post);
} else {
// If the post does not exist, send a 404 status code and a message
});
// This middleware parses the request body as JSON and adds it to the req object
app.use(express.json());
posts.push(newPost);
// Send a 201 status code and the new post as a JSON response
res.status(201).json(newPost);
} else {
res.status(400).send('Invalid data');
});
app.use(express.json());
const id = req.params.id;
// If the data is valid, find the post with the given id in the posts array
if (post) {
post.title = data.title;
post.content = data.content;
post.author = data.author;
// Send a 200 status code and the updated post as a JSON response
res.status(200).json(post);
} else {
// If the post does not exist, send a 404 status code and a message
} else {
res.status(400).send(‘Invalid data’);
To test the API, you can use a tool like Postman, which is a software that allows you to send and receive
HTTP requests and responses. You can download Postman from official website and follow the
instructions for your operating system.
Select the HTTP method that matches the route you want to test, such as GET, POST, PUT, or DELETE
Enter the URL for the route you want to test, such as http://localhost:5000/posts or
http://localhost:5000/posts/1
If the route requires a request body, such as POST or PUT, click on the Body tab and select the raw
option and the JSON format
Enter the data for the request body in JSON format, such as {"title": "New Post", "content": "This is a
new post", "author": "Dan"}
Click on the Send button and see the response in the lower panel
You can also see the status code, the headers, and the time of the response in the upper panel
You can test different scenarios and see how the API behaves. For example, you can try to create a new
post, update an existing post, delete an existing post, get a list of all posts, get a single post, or get a
non-existing post. You can also try to send invalid data or invalid URLs and see the error messages.
Conclusion
In this blog, I have shown you how to create a simple backend API in Express JS that can handle GET and
POST requests, and return JSON data. You have learned how to install Node JS and Express JS, how to
create a project folder and a server file, how to define the data model and the data array, how to create
the routes and the handlers for the API, and how to test the API with a tool like Postman. I hope you
have enjoyed this blog and learned something new. If you have any questions or feedback, please leave
a comment below. Thank you for reading!