我学习 ExpressJS,但 next() 不起作用。请帮忙。
我正在观看有关 Express JS 的 Udemy 课程。我像讲师的代码一样编码,但我得到了这样的错误。
expressJS 版本:4.17.1
index.js:
const express = require("express");
const app = express();
const port = 1014;
app.use("/", (req, res, next) => {
res.send("<h1>Home page</h1>");
console.log("/ Ok");
next();
});
app.use("/about", (req, res, next) => {
res.send("<h1>About page</h1>");
console.log("/about ok");
});
app.listen(port, () => console.log("Sunucu Aktif!: http://localhost:" + port + "/"));
当我进入 /about 页面时出现此错误。
[nodemon] restarting due to changes...
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
Sunucu Aktif!: http://localhost:1014/
/ Ok
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:526:11)
at ServerResponse.header (C:\projects\expresslesson\node_modules\express\lib\response.js:771:10)
at ServerResponse.send (C:\projects\expresslesson\node_modules\express\lib\response.js:170:12)
at C:\projects\expresslesson\index.js:12:9
at Layer.handle [as handle_request] (C:\projects\expresslesson\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\projects\expresslesson\node_modules\express\lib\router\index.js:317:13)
at C:\projects\expresslesson\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\projects\expresslesson\node_modules\express\lib\router\index.js:335:12)
at next (C:\projects\expresslesson\node_modules\express\lib\router\index.js:275:10)
at C:\projects\expresslesson\index.js:8:6
/ Ok
您认为代码中的问题和解决方案是什么,我正在等待您的回复。