In nodejs, routing refers to the mapping relationship between url address and response function; a url address responds to an html page, which is to extract the business of a path matching relationship into a separate js file. The syntax for creating a route is " let router = express.Router();".
The operating environment of this article: Windows 10 system, nodejs version 12.19.0, Dell G3 computer.
Broadly speaking, routing is a mapping relationship.
Routing in nodejs is actually the mapping relationship between URL addresses and response functions. A URL address responds to an HTML page.
It is to extract the business of a path matching relationship into a separate js file.
Configure and use
/routes/xx.js
// 1. Create a route let router = express.Router(); //2 Route processing response router. Response API (address, processing function) // 3. Export routing module.exports = router;/app.jsmainservice
//Install routing app.use('address',router);/routes/xx.js
//Install routing in sub-routing nested router.use('address', sub-router) //Need next continuation //Intercept some public services under the current route router.all('*', verification work under the current router route ) //Need next continuationExpand your knowledge:
Introduction to Express
Based on the Node.js platform, a fast, open and minimalist web development framework
1. The function of Express is similar to the built-in http module of Node.js, which is specially used to create web servers.
2. The essence of Express: It is a third-party package on npm, which provides a convenient method to quickly create a web server.
Learn more about express
Thinking: Is it possible to create a Web server without using Express?
Answer: Yes, you can use the native http module provided by Node.js.
Thinking: How can you be bright if you are good at it (with the http built-in module, why do you still use Express)?
Answer: The built-in http module is very complicated to use and the development efficiency is low; Express is further encapsulated based on the built-in http module, which can greatly improve development efficiency.
Thinking: What is the relationship between the http built-in module and Express?
Answer: Similar to the relationship between Web API and jQuery in the browser. The latter is further encapsulated based on the former.
Recommended learning: "nodejs video tutorial"
The above is the detailed content of nodejs routing. Please pay attention to other related articles on this site for more information!