您可以作为 Express 链中的第一个中间件插入一个中间件处理程序,该处理程序检查请求类型,然后通过向其添加前缀路径将 req.url 修改为伪 URL。然后,此修改将强制该请求仅发送到特定路由器(设置为处理该特定 URL 前缀的路由器)。我已使用以下代码在 Express 中验证了此功能:
var express = require('express');
var app = express();
app.listen(80);
var routerAPI = express.Router();
var routerHTML = express.Router();
app.use(function(req, res, next) {
// check for some condition related to incoming request type and
// decide how to modify the URL into a pseudo-URL that your routers
// will handle
if (checkAPICall(req)) {
req.url = "/api" + req.url;
} else if (checkHTMLCall(req)) {
req.url = "/html" + req.url;
}
next();
});
app.use("/api", routerAPI);
app.use("/html", routerHTML);
// this router gets hit if checkAPICall() added `/api` to the front
// of the path
routerAPI.get("/", function(req, res) {
res.json({status: "ok"});
});
// this router gets hit if checkHTMLCall() added `/api` to the front
// of the path
routerHTML.get("/", function(req, res) {
res.end("status ok");
});
注意:我没有填写 checkAPICall() 或 checkHTMLCall() 的代码,因为您没有完全具体说明您希望它们如何工作。我在自己的测试服务器中模拟了它们,以查看这个概念是否有效。我假设您可以为这些函数提供适当的代码或替换您自己的 if 语句。
之前的答案
我刚刚验证了您可以在 Express 中间件中更改 req.url,因此如果您有一些修改了 req.url 的中间件,则会影响该请求的路由。
// middleware that modifies req.url into a pseudo-URL based on
// the incoming request type so express routing for the pseudo-URLs
// can be used to distinguish requests made to the same path
// but with a different request type
app.use(function(req, res, next) {
// check for some condition related to incoming request type and
// decide how to modify the URL into a pseudo-URL that your routers
// will handle
if (checkAPICall(req)) {
req.url = "/api" + req.url;
} else if (checkHTMLCall(req)) {
req.url = "/html" + req.url;
}
next();
});
// this will get requests sent to "/" with our request type that checkAPICall() looks for
app.get("/api/", function(req, res) {
res.json({status: "ok"});
});
// this will get requests sent to "/" with our request type that checkHTMLCall() looks for
app.get("/html/", function(req, res) {
res.json({status: "ok"});
});
旧答案
我能够像这样成功地将请求回调放在 express 前面,并看到它成功地修改了传入的 URL,然后像这样影响 express 路由:
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(function(req, res) {
// test modifying the URL before Express sees it
// this could be extended to examine the request type and modify the URL accordingly
req.url = "/api" + req.url;
return app.apply(this, arguments);
});
server.listen(80);
app.get("/api/", function(req, res) {
res.json({status: "ok"});
});
app.get("/html/", function(req, res) {
res.end("status ok");
});
这个例子(我测试过)只是硬连线在 URL 的前面添加“/api”,但您可以自己检查传入的请求类型,然后根据需要进行 URL 修改。我还没有探索这是否可以完全在 Express 中完成。
在这个例子中,当我请求“/”时,我得到了 JSON。