1////////////////////////////
var express = require("express");
var app = express();
var apiRouters = express.Router(); //这里定义了一个路由
apiRouters.get("/index",function(req,res){
res.send("123"})
})
app.use("/api",apiRouters) //调用app.use的api路由才能调用apiRouters路由
2///////////////////////
var express = require("express");
var app = express();
//这里没有定义一个路由
app.get("/api",function(req,res){
res.send("111"})
})
//这里没有调用
The first 127.0.0.1:**/api/index can access 123
The second 127.0.0.1:*/api can access 111
The first code is different from the second code but they can achieve the same effect.
Why is this, or am I understanding it wrong somewhere?
为情所困2017-05-16 13:44:27
use("/api", processor)
: 相对路径中以'/api'开头的url都可以被 use截获到,而且不关心请求的方式是get还是post。"http://yourdomain/api", "http://yourdomain/api/index"都可以被它捕获到。get("/api", processor)
: The relative path must be equal to '/api' to be captured, only "http://yourdomain/api" can be captured, and the request method is "get".
过去多啦不再A梦2017-05-16 13:44:27
Express router itself is a middleware, just like its name. Its task is to distribute different requests to different routes. If you want to do API version control, you may have two routes: /api/v1/user and /api/v2/user. At this time, the router calls different APIs based on the routing settings. Express itself is an object, and operations on it are feasible, but not standardized