考虑这个小应用程序:
var express = require("express");
var app = express();
app.get("/json", function(req, res){
console.log("JSON route");
res.json({foo: "bar"});
});
app.get("/", function(req, res){
console.log("Slash route");
res.send("Hello");
});
app.get("*", function(req, res){
console.log("Star route");
res.redirect("/");
});
app.listen(3000, function(){
console.log("Listening.");
});
每当我在浏览器中转到localhost:3000
或localhost:3000/json
时,我都会从服务器日志中看到星号*
路线也会被触发。如果将其更改为app.get("/*")
这是为什么?我以为res.send
和res.json
停止了执行。
答案 0 :(得分:2)
当您向浏览器请求某些内容时,它会自动尝试请求图标。由于您的服务器没有为favicon定义路由,因此它会转到*
路由,从而导致这种混淆。