refer to
project | describe |
---|---|
Bilibili | dark horse programmer |
search engine | Bing |
describe
project | describe |
---|---|
Edge | 109.0.1518.61 (official version) (64-bit) |
NodeJS | v18.13.0 |
nodemon | 2.0.20 |
Express | 4.18.2 |
routing
In the Web, routing can be understood as a mapping relationship between URL s and functions. When a request is sent from the client to the server, it needs to be matched with the defined route. When the match is successful, the corresponding callback function will be called to respond to the client's request.
Route Matching Rules
The successful match between the URL and the route needs to satisfy the request type of the request sent by the client and the path requested by the request to match the corresponding settings of the route.
Take a chestnut:
// Contains the NodeJS third-party module express const express = require('express'); // Create server object const app = express(); // Listen to port 9090 of this machine app.listen(9090); // create route app.get('/', (req, res) => { res.send('GET /'); }); app.get('/user', (req, res) => { res.send('GET /user'); }); app.post('/user/RedHeart', (req, res) => { res.send('POST /user/RedHeart'); });
Note:
In the above example, the established routes will be successfully matched with the following types of requests in turn:
- The client requests 127.0.0.1:9090/ through a GET request.
- The client requests 127.0.0.1:9090/user through a GET request.
- The client requests 127.0.0.1:9090/user/RedHeart through a POST request.
sequential match
The requested URL will be sequentially matched with the routes established in the program, and the matching will stop once the matching is successful.
// Contains the NodeJS third-party module express const express = require('express'); const app = express(); // Listen to port 9090 of this machine app.listen(9090); // create route app.get('/', (req, res) => { res.send('First'); }); app.get('/', (req, res) => { res.send('Second'); });
Results of the:
Modular
Express does not recommend directly mounting the route to express(), that is, the app, as in the previous example, but recommends extracting the route separately and using it as a module.
create
First, we need to create a JavaScript file to store the routing module, and add the following content to the file to create the routing module.
const express = require('express'); const router = express.Router(); // create route router.get('/', (req, res) => { res.send('GET /'); }) router.get('/user', (req, res) => { res.send('GET /user'); }) // export the route module.exports = router;
use
// Import NodeJS built-in module path for splicing paths const path = require('path'); // Import the routing module located in the current working directory const router = require(path.join(__dirname, './navigator')); // Import NodeJS third-party module Express const express = require('express'); const app = express(); // Listen to port 9090 of this machine app.listen(9090); // Register the imported routing module app.use(router);
After registering the routing module, the routing starts working normally.
In this example, visiting the following link you will get the following response.
Link | response |
---|---|
127.0.0.1:9090/ | GET / |
127.0.0.1:9090/user | GET /user |
prefix
You may want to add a unified prefix to the created route, here we try to add a unified prefix RedHeart to the route. In this example, we only modify one line of code, otherwise it is the same as the previous example.
Will
app.use(router);
change into
app.use('/RedHeart', router);
After modification, in this example, you will get the following response when visiting the link below.
Link | response |
---|---|
127.0.0.1:9090/RedHeart | GET / |
127.0.0.1:9090/redheart/user | GET /user |
Note:
It is case-insensitive in the URL, so whether you visit 127.0.0.1:9090/RedHeart or 127.0.0.1:9090/redheart, the result will be the same.