Getting Started with Express
Tags: programming, javascript
Published: 2021-07-11 19:16:10

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvfxmq90jmrfcgxujh00.png)

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](https://nodejs.org/en/)
- An editor that you like working in. I use VS Code, you can install it from [here](https://code.visualstudio.com/download)
- Postman installed. Install it from [here](https://www.postman.com/)

First of all, we are going to prepare our work environment.Let's start by creating our folder 

```jsx
C:\Users\ameni>mkdir our_project
```

```jsx
C:\Users\ameni>cd our_project
```

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

```jsx
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 

```jsx
C:\Users\ameni\our_project>code
```

Your terminal now should look like this 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/knyxsvc2bjxcgmxr8mjn.PNG)

- Let's start now by installing our Express framework

open the terminal and run this command 

```jsx
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!

```jsx
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.

```jsx
const app = require("./index");
const port = 8000;

app.listen(port, () => {

console.log(`App running on ${port}...`);

});
```

Now let's run our server 

```jsx
node server.js
```

In [http://localhost:8000/](http://localhost:8000/) you will see Hello World 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2fv5r5wpw49jokkmag4i.PNG)

- 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

 

```jsx
app.get("/person", (req, res) => {
  res.json(person);
});
```

```jsx
app.post("/person", (req, res) => {
  const body = req.body;
  person.push(body);
  res.json(body);
});
```

```jsx
app.delete("/person/:id", (req, res) => {
  const id = req.params.id;
  const filtredPerson = person.filter((value, index) => index != id);
  person = filtredPerson;
  res.json(filtredPerson);
});
```

```jsx
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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahz8w1n8i7cw8xbq3qac.png)

To add a person we send this post request . 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wp7mqrfuc777lqaum3h1.png)

To get the data we have we sent this request.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e5pkmoolxrugs72xcykw.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t4kifw9aiivpgxuc1ter.png)

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

```jsx
app.get("/person", (req, res) => {
  const { personId } = req.query;
  person[personId] = body;
  res.json(Person);
});
```

- In the Postman
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jdp1njo2jv7ja7ohq0z.png)

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.