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 the mapping relationship.
The routing in nodejs is actually the mapping relationship between the url address and the response function. A url address responds to an html page.
Is to extract the business of a path matching relationship into a separate js file.
Configuration and use
/routes/xx.js
// 1. 创建路由 let router = express.Router(); //2 路由处理响应 router.响应API(地址, 处理函数) //3. 导出路由 module.exports = router;
/app.js Main Service
//安装路由 app.use('地址',router);
/routes/xx.js
//子路由里安装路由 嵌套 router.use('地址',子router) //需要next 延续 //截获当前路由下的部分公共业务 router.all('*',当前router路由下的验证工作) //需要next 延续
Extended knowledge:
Express Introduction
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, and is specially used to create web servers. of.
2. The essence of Express: It is a third-party package on npm that provides a convenient method to quickly create a web server.
Learn more about express
Thinking: Can I create a web server without using Express?
Answer: Yes, just use the native http module provided by Node.js.
Thinking: If you are already good, why should you be bright (with the http built-in module, why do you still use Express)?
Answer: The built-in http module is very complicated to use and has low development efficiency; 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 What is routing in nodejs. For more information, please follow other related articles on the PHP Chinese website!