3

Getting Started with Express

 4 years ago
source link: https://dev.to/amenibensaada/getting-started-with-exrpess-4plg
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Getting Started with Express

Jul 11

・4 min read

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

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

C:\Users\ameni\our_project> npm init -y
Enter fullscreen modeExit 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 modeExit fullscreen mode

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

Now let's run our server

node server.js
Enter fullscreen modeExit fullscreen mode

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);
});
Enter fullscreen modeExit fullscreen mode
app.post("/person", (req, res) => {
  const body = req.body;
  person.push(body);
  res.json(body);
});
Enter fullscreen modeExit 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 modeExit 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 modeExit fullscreen mode
  • 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);
});
Enter fullscreen modeExit 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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK