DEV Community

loading...

Getting Started with Express

Ameni Ben Saada
Junior front end developer
・4 min read

Alt Text

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
Enter fullscreen mode Exit fullscreen mode
C:\Users\ameni>cd our_project
Enter fullscreen mode Exit fullscreen mode

Now use this command to create a new node.js project.

C:\Users\ameni\our_project> npm init -y
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Your terminal now should look like this

Alt Text

  • Let's start now by installing our Express framework

open the terminal and run this command

npm install express
Enter fullscreen mode Exit fullscreen mode
  • 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;
Enter fullscreen mode Exit fullscreen mode
  • 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}...`);

});
Enter fullscreen mode Exit fullscreen mode

Now let's run our server

node server.js
Enter fullscreen mode Exit fullscreen mode

In http://localhost:8000/ you will see Hello World

Alt Text

  • 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);
});
Enter fullscreen mode Exit fullscreen mode
app.post("/person", (req, res) => {
  const body = req.body;
  person.push(body);
  res.json(body);
});
Enter fullscreen mode Exit fullscreen mode
app.delete("/person/:id", (req, res) => {
  const id = req.params.id;
  const filtredPerson = person.filter((value, index) => index != id);
  person = filtredPerson;
  res.json(filtredPerson);
});
Enter fullscreen mode Exit fullscreen mode
app.patch("/person/:id", (req, res) => {
  const id = req.params.id;
  const body = req.body;
  person[id] = body;
  res.json( person);
});
Enter fullscreen mode Exit fullscreen mode
  • Now to test our code we are going to use Postman

Alt Text

To add a person we send this post request .

Alt Text

To get the data we have we sent this request.

Alt Text

To update the age of this person we sent a patch request.

Alt Text

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);
});
Enter fullscreen mode Exit fullscreen mode
  • In the Postman Alt Text

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.

Discussion (4)

Collapse
aidanbek profile image
aidanbek

Still waiting for v5, v4 has some issues with memory leaks

Collapse
amenibensaada profile image
Ameni Ben Saada Author

I agree

Collapse
cenacr007_harsh profile image
KUMAR HARSH

Very nice introduction to Rest api.

Collapse
amenibensaada profile image
Ameni Ben Saada Author

Glad you like it thank you