Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 11, 2021 07:16 pm GMT

Getting Started with Express

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 fromhere
  • An editor that you like working in. I use VS Code, you can install it from here
  • Postman installed. Install it fromhere

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

Alt Text

  • 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!
constexpress=require("express");constapp=express();app.use(express.json());app.use("/",(req,res)=>res.send("Helloworld!"));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

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);});
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

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);});
  • In the PostmanAlt 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.


Original Link: https://dev.to/amenibensaada/getting-started-with-exrpess-4plg

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To