Node.js is an open-source and cross-platform runtime environment, that allows you to write JavaScript on the server which is on of the most helpful advantages of Node.js since the same programming language can be used for server-side and client-side applications. It also runs on V8 JavaScript engine which makes it highly performant.
And one of the most popular frameworks for NodeJs is our subject today in this blog: Express Express is written on top of node.js to make writing server-side javascript easier.
So what we are going to do today is building a very basic REST API.
Before we start, make sure that you have these if you don't please install them.
- Node.js installed. You can install it from here
- An editor that you like working in. I use VS Code, you can install it from here
- Postman installed. Install it from here
First of all, we are going to prepare our work environment.Let's start by creating our folder
C:\Users\ameni>mkdir our_project
C:\Users\ameni>cd our_project
Now use this command to create a new node.js project.
C:\Users\ameni\our_project> npm init -y
All we have to do now is opening our folder in the editor. For VS Code you can use this command
C:\Users\ameni\our_project>code
Your terminal now should look like this
- Let's start now by installing our Express framework
open the terminal and run this command
npm install express
- Now we are going to create a file named index.js and starting our code.So we are going to import express and use express.json and send a Hello world text and finally we exported it!
const express = require("express");
const app = express();
app.use(express.json());
app.use("/", (req, res) => res.send("Hello world!"));
module.exports = app;
- Now we are going to create another file,to keep our code clean, named server.js . We are going to import our app module and created the port which the application will work on and we started or app.
const app = require("./index");
const port = 8000;
app.listen(port, () => {
console.log(`App running on ${port}...`);
});
Now let's run our server
node server.js
In http://localhost:8000/ you will see Hello World
- Now what we are going to do is create something more interesting by building a simple application to create, read, update, and delete Person's information.
To do that we are going to use Post, Get, Patch, and Delete methods.
- So, We will create a variable named person Which contains an array of persons
- With the get request, we will retrieve all the persons and send them as a response
- We will use the post request to push a new person to the person array
- The delete Method to delete a person by filtering the person array by person id
- And finally, we will update a person by using the patch request and the person id
app.get("/person", (req, res) => {
res.json(person);
});
app.post("/person", (req, res) => {
const body = req.body;
person.push(body);
res.json(body);
});
app.delete("/person/:id", (req, res) => {
const id = req.params.id;
const filtredPerson = person.filter((value, index) => index != id);
person = filtredPerson;
res.json(filtredPerson);
});
app.patch("/person/:id", (req, res) => {
const id = req.params.id;
const body = req.body;
person[id] = body;
res.json( person);
});
- Now to test our code we are going to use Postman
To add a person we send this post request .
To get the data we have we sent this request.
To update the age of this person we sent a patch request.
And finally to delete the data we sent a delete request .
In all the above, we sent our request in the body now what if we want to use query ?
All we have to do is this :
- In the code
app.get("/person", (req, res) => {
const { personId } = req.query;
person[personId] = body;
res.json(Person);
});
- In the Postman
So here we got our data using req.query instead of sending the request in the body.
And that's it for this blog. I hope you did learn a thing or two from reading and practicing this.
If there's anything wrong with this article, please let me know.I would love to correct and improve it.
Top comments (4)
Still waiting for v5, v4 has some issues with memory leaks
I agree
Very nice introduction to Rest api.
Glad you like it thank you