【发布时间】:2016-12-14 23:56:08
【问题描述】:
我将保持代码简单,但基本上,我有一个运行 Express 的 Node.js 服务器。 Express 使用池(和 mysql npm 包)连接到 MySQL 数据库。这就是我的服务器的样子(我省略了无聊的要求和保持简单的东西)。这是服务器处理的唯一路由。
server.all('/test', function (req, res, next) {
pool.getConnection(function (err, conn) {
if (err) {
console.log(err);
}
else {
conn.query("select * from spwp_appusers where id=46", function (err, rows) {
conn.release();
if (err) {
console.log(err);
}
else {
res.send(200);
}
});
}
});
next();
});
但是,当我运行此代码时,服务器尝试执行 res.send(200); 但中断并出现以下错误:
/usr/lib/node_modules/mysql/lib/protocol/Parser.js:77
throw err; // Rethrow non-MySQL errors
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
有人知道发生了什么吗?为什么我不能发送响应?即使我使用回调,我也会收到此错误。
【问题讨论】: