我对Node.js相当陌生,遇到了一些问题。
我正在使用Node.js 4.10和Express 2.4.3。
当我尝试访问http://127.0.0.1:8888/auth/facebook时,我将重定向至http://127.0.0.1:8888/auth/facebook_callback。
然后,我收到以下错误:
Error: Can't render headers after they are sent to the client.
at ServerResponse.<anonymous> (http.js:573:11)
at ServerResponse._renderHeaders (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:64:25)
at ServerResponse.writeHead (http.js:813:20)
at /home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/auth.strategies/facebook.js:28:15
at /home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/index.js:113:13
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/strategyExecutor.js:45:39)
at [object Object].pass (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/authExecutionScope.js:32:3)
at [object Object].halt (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/authExecutionScope.js:29:8)
at [object Object].redirect (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/authExecutionScope.js:16:8)
at [object Object].<anonymous> (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/auth.strategies/facebook.js:77:15)
Error: Can't set headers after they are sent.
at ServerResponse.<anonymous> (http.js:527:11)
at ServerResponse.setHeader (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:50:20)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:162:13)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:195:11)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:150:23)
at param (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/router.js:189:13)
at pass (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/router.js:191:10)
at Object.router [as handle] (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/router.js:197:6)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:198:15)
at Object.auth [as handle] (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/index.js:153:7)
Error: Can't set headers after they are sent.
at ServerResponse.<anonymous> (http.js:527:11)
at ServerResponse.setHeader (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:50:20)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:162:13)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:207:9)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:150:23)
at param (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/router.js:189:13)
at pass (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/router.js:191:10)
at Object.router [as handle] (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/router.js:197:6)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:198:15)
at Object.auth [as handle] (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/index.js:153:7)
Error: Can't set headers after they are sent.
at ServerResponse.<anonymous> (http.js:527:11)
at ServerResponse.setHeader (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:50:20)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:162:13)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:150:23)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:207:9)
at Object.auth [as handle] (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/index.js:153:7)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:198:15)
at HTTPServer.handle (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:211:3)
at Object.handle (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:105:14)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:198:15)
Error: Can't set headers after they are sent.
at ServerResponse.<anonymous> (http.js:527:11)
at ServerResponse.setHeader (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:50:20)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:162:13)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:150:23)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:207:9)
at HTTPServer.handle (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:211:3)
at Object.handle (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:105:14)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:198:15)
at /home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/session.js:323:9
at /home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/session.js:338:9
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Can't set headers after they are sent.
at ServerResponse.<anonymous> (http.js:527:11)
at ServerResponse.setHeader (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:50:20)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:162:13)
at next (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:207:9)
at /home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/session.js:323:9
at /home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/session.js:338:9
at Array.<anonymous> (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/session/memory.js:57:7)
at EventEmitter._tickCallback (node.js:126:26)
以下是我的代码:
var fbId= "XXX";
var fbSecret= "XXXXXX";
var fbCallbackAddress= "http://127.0.0.1:8888/auth/facebook_callback"
var cookieSecret = "node"; // enter a random hash for security
var express= require('express');
var auth = require('connect-auth')
var app = express.createServer();
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({secret: cookieSecret}));
app.use(auth([
auth.Facebook({
appId : fbId,
appSecret: fbSecret,
callback: fbCallbackAddress,
scope: 'offline_access,email,user_about_me,user_activities,manage_pages,publish_stream',
failedUri: '/noauth'
})
]));
app.use(app.router);
});
app.get('/auth/facebook', function(req, res) {
req.authenticate("facebook", function(error, authenticated) {
if (authenticated) {
res.redirect("/great");
console.log("ok cool.");
console.log(res['req']['session']);
}
});
});
app.get('/noauth', function(req, res) {
console.log('Authentication Failed');
res.send('Authentication Failed');
});
app.get('/great', function( req, res) {
res.send('Supercoolstuff');
});
app.listen(8888);
我可以知道我的代码有什么问题吗?
res
Express中的对象是Node.js的http.ServerResponse
子类(请阅读http.js源代码)。允许您拨打任意数量的电话res.setHeader(name, value)
,直到致电为止res.writeHead(statusCode)
。之后writeHead
,将标题放进去,您只能调用res.write(data)
,最后res.end(data)
。
错误“错误:发送标头后无法设置标头”。表示您已经处于“正文”或“完成”状态,但是某些函数试图设置标头或statusCode。当您看到此错误时,请尝试查找在某些正文已被写入之后尝试发送标头的任何内容。例如,查找意外调用两次的回调,或发送正文后发生的任何错误。
在您的情况下,您致电res.redirect()
,这导致响应变为“完成”。然后,您的代码引发了错误(res.req
is
null
)。并且由于该错误发生在您的实际错误function(req, res, next)
(而不是回调)之内,因此Connect能够捕获该错误,然后尝试发送500错误页面。但是,由于已经发送了标头,所以Node.js
setHeader
抛出了您看到的错误。
回复必须在 标题中, 并保持在 标题中 :
响应必须在头脑中,然后变成身体:
res.writeHead(statusCode, [reasonPhrase], [headers])
响应可以是头部/身体,也可以是身体:
res.write(chunk, encoding='utf8')
响应可以是Head / Body,也可以是Finished:
res.end([data], [encoding])
响应可以处于Head / Body,也可以保持其当前状态:
res.addTrailers(headers)
响应必须在标题中,并已完成:
问题内容: 我对Node.js相当陌生,遇到了一些问题。 我正在使用Node.js 4.10和Express 2.4.3。 当我尝试访问http://127.0.0.1:8888/auth/facebook时,我将重定向到http://127.0.0.1:8888/auth/facebook_callback。 然后,我收到以下错误: 以下是我的代码: 我可以知道我的代码有什么问题吗? 问题答案:
所以我看到了一篇很棒的帖子“错误:发送到客户端后无法设置标题,但仍然不明白我的“标题”部分有什么问题,因为当我将它从注释掉的
问题内容: 将标头发送到客户端后,无法设置标头。这是请求验证后的后端错误,看起来像标头问题。我为我的项目脏代码感到抱歉,我还需要做其他事情,因此在注释中有一些代码。这是我的代码 您可以在我的代码中发现其他错误,因为我尝试了许多不同的方法。这是passport.js配置文件 这是身份验证的ajax请求 非常感谢你。 问题答案: 这是由于您的代码试图从authenticate函数发送多个响应。 删除其
将标头发送到客户端后,无法对其进行设置。这是我的身份验证post请求后的后端错误。看起来标题有问题。我很抱歉我的项目肮脏的代码,我需要做一些别的事情,所以有一些代码在评论中。这是我的代码 你可以在我的代码中找到其他错误,因为我尝试了许多不同的方法。以下是passport.js配置文件 这是用于身份验证的 ajax 请求 非常感谢。
我在NodeJS中使用Passport.js、Express和Mongoose时遇到了这个奇怪的问题。基本上,即使我只发送了一个头,我还是得到一个错误,说“不能在头被发送到客户端后设置它们”。 我读过其他帖子并尝试过,但没有一个有效。 应用程序。get-res.send与return res.send之间有什么区别吗 错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后,无法设
我正在关注一个关于udemy上的节点.js的课程,这有点过时了,遇到了这些错误,我无法找到解决方案。 我尝试了什么: 使用next(); 在所有if语句中添加返回res 有人能帮我修一下这些吗?我将不胜感激! 提前感谢! 用户名存在错误:错误 [ERR_HTTP_HEADERS_SENT]:在服务器响应.set 上将标头发送到客户端后无法设置标头 (_http_outgoing.js:526:11