forked from faisal2410/terminology
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_routing_controller_middleware.js
More file actions
73 lines (43 loc) · 2.79 KB
/
Copy path7_routing_controller_middleware.js
File metadata and controls
73 lines (43 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
HTTP Routing:
Routing in the MERN stack refers to the process of mapping incoming HTTP requests to specific functions, called controllers, that handle the request and return a response. This is usually done using a router, which is a component that defines the endpoints of your application and the corresponding controller functions that should be executed when those endpoints are accessed.
Here's an example of how to set up a router in an Express.js app:
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
res.send('Hello, world!');
});
router.post('/user', (req, res) => {
// Handle POST request to create a new user
});
module.exports = router;
In this example, we're creating a router that defines two endpoints: a GET endpoint that returns a simple "Hello, world!" message, and a POST endpoint that will be used to create a new user. The router.get() and router.post() functions define the routes and the corresponding callback functions to execute when those routes are accessed.
Controllers:
Controllers are functions that handle the business logic of your application. In the context of the MERN stack, controllers are often used to handle HTTP requests by processing the request data, interacting with the database, and returning an appropriate response. Here's an example of a simple controller function:
const User = require('../models/user');
exports.getUserById = (req, res) => {
User.findById(req.params.id, (err, user) => {
if (err) {
res.status(500).send(err);
} else {
res.send(user);
}
});
};
In this example, we're defining a controller function that handles a GET request for a user with a specific ID. The function retrieves the user data from the database using the User.findById() method and returns it in the response.
Middleware:
Middleware are functions that can be executed before or after a request is processed by the router or controller functions. Middleware functions can modify the request or response objects, or perform other tasks such as logging or authentication.
Here's an example of a simple middleware function:
exports.logger = (req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
};
In this example, we're defining a middleware function that logs the HTTP method and URL of each request. The next() function is called to pass control to the next middleware function or route handler.
To use this middleware in an Express.js app, you would call the app.use() function like this:
const express = require('express');
const app = express();
const { logger } = require('./middleware');
app.use(logger);
This code applies the logger middleware to all requests to your Express app.
I hope this helps you understand HTTP routing, controllers, and middleware in the MERN stack!
*/