最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

next()在Express 4上不起作用,错误[ERR

IT培训 admin 12浏览 0评论

next()在Express 4上不起作用,错误[ERR

我学习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

您认为代码中的问题和解决方案是什么,我正在等待您的答复。

回答如下:

TLDR

如@goto所述,在调用res.send()之后不应立即跟随next()

更长

请参阅res.send,因为服务器将输出回请求者。调用next之后,您要告诉Express将请求转发到下一个中​​间件,从技术上讲,这是错误中间件。

在Express中,当请求到来时,它会寻找与URL和方法匹配的中间件。如果未找到,则将其转发到下一个中​​间件。最后一个是错误中间件,错误被抛出给用户。

next()在Express 4上不起作用,错误[ERR

我学习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

您认为代码中的问题和解决方案是什么,我正在等待您的答复。

回答如下:

TLDR

如@goto所述,在调用res.send()之后不应立即跟随next()

更长

请参阅res.send,因为服务器将输出回请求者。调用next之后,您要告诉Express将请求转发到下一个中​​间件,从技术上讲,这是错误中间件。

在Express中,当请求到来时,它会寻找与URL和方法匹配的中间件。如果未找到,则将其转发到下一个中​​间件。最后一个是错误中间件,错误被抛出给用户。

发布评论

评论列表 (0)

  1. 暂无评论