Express 忽略 response.send() 调用

Express ignores response.send() call

我有一个简单的服务器。代码解释如下:

const express = require("express");
 
const app = express();
 
app.set("view engine", "hbs");
 
app.get("/about-me", function(_, response) {
    response.render("about_me.hbs");
});
app.get("/", function (_, response) {
    response.render("index.hbs");
});
app.all(/.*/, function (_, response) { // 404 page
    response.sendStatus(404); // set status code to 404
    response.send("<h1>404 Not Found</h1>"); // send a body
}
app.listen(3000);

但是当我打开 http://localhost:3000/not-exist 时,出现了“Not Foun<”字样。为什么会这样?

您不能对一个请求发送两个响应。

使用sendStatus or send.

如果想在使用send时控制状态码,使用status