Skip to content

middlewares in express #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ A very simple Web API, using JSON data from a file. We have divided the process
- In this part, We have implemented basic routing using Express.js .

- To Learn more about how to implement Routing in Node using Express.js, you may refer this <a href="https://iq.opengenus.org/p/20e3950d-43f5-45ce-b2f6-2167c63a601b/"> article</a>.

## 🚀 <a href="https://github.com/OpenGenus/web_api_nodejs/tree/master/part_5_express_middleware">Part 5</a>

- In this part, We have included middlewares in our Express app.

- To Learn more about middlewares in Express.js, you may refer this <a href="https://iq.opengenus.org/p/d1f54e16-0990-4eac-aebd-b60f92055dc4/"> article</a>.
44 changes: 44 additions & 0 deletions part_5_express_middlewares/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# web_api_nodejs

# Part 5: Middlewares in Express.js

## ⭐ What is a Middleware ?

Middlewares are functions that are executed in between i.e in the middle of receiving a request and sending back a response. They are used to manipulate request or response object or to execute any other code.

Middleware functions can perform the following tasks:

- Execute any code.
- Make changes to the request and the response objects.
- End the request-response cycle.
- Call the next middleware function in the stack.

In this part, we have included some middlewares in our Express app.

To understand more about creating the web server, you may refer <a href="https://github.com/OpenGenus/web_api_nodejs/tree/master/part_1">part 1</a>.

To understand more about implementation of Routes in Node, you may refer <a href="https://github.com/OpenGenus/web_api_nodejs/tree/master/part_2">part 2</a>.

To understand more about creating a Web Api in Node, you may refer <a href="https://github.com/OpenGenus/web_api_nodejs/tree/master/part_3">part 3</a>.

To understand more about implementing routing in Express.js , you may refer <a href="https://github.com/OpenGenus/web_api_nodejs/tree/master/part_4_express">part 4</a>.

## 🚀 Get Started

1. Clone or download the repository.

2. Once you've downloaded the repo, Open it in an editor of your choice.

3. Open the terminal and traverse to **part_5_express_middleware** directory.

4. Now run command `npm install` to install all the required dependencies.

5. Now run command `node app.js` to start the server.

6. Open Postman and go to `127.0.0.1:3000` to see your server running.

7. Go to `GET 127.0.0.1:3000`, `POST 127.0.0.1:3000/data`, `GET 127.0.0.1:3000/some-random-route` to test the working of the middlewares.

## 📝 Learn More

To Learn more about how to include middlewares in Express.js, you may refer this <a href="https://iq.opengenus.org/p/d1f54e16-0990-4eac-aebd-b60f92055dc4/"> article</a>.
39 changes: 39 additions & 0 deletions part_5_express_middlewares/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Require express

const express = require("express");
const app = express();
const morgan = require("morgan");

app.use(express.json());
app.use(morgan("dev"));

// Middleware
app.use((req, res, next) => {
console.log("Hello from the middleware !");
next();
});

// Listening to server
const port = 3000;
app.listen(3000, () => {
console.log(`App running on port ${port}`);
});

// Routes

app.get("/", (req, res) =>
res
.status(200)
.json({ message: "Hello from the server !", app: "Express-Routes" })
);

// Middleware
app.use((req, res, next) => {
console.log("Hello from the middleware defined after the route !");
next();
});

app.post("/data", (req, res) => {
console.log(req.body);
res.status(200).json({ status: "Success !", data: { body: req.body } });
});
Loading