本文介绍了错误:发送到客户端后,不能设置头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我对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 。 然后我收到以下错误: code>错误:发送到客户端后,不能将头文件呈现。 在ServerResponse。< anonymous> (http.js:573:11)在ServerResponse._renderHeaders(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:64:25)在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)在[object Object] .pass(/ home / eugene / public_html / all_things_node / projects / fb2 / node_modules / connect-auth / /authExecutionScope.js:32:3)在[object Object] .halt(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/authExecutionScope.js:29:8) $ object = ecutionScope.js:16:8)在[object Object]。< anonymous> (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/auth.strategies/facebook.js:77:15)错误:发送后不能设置标题。 在ServerResponse。< anonymous> (http.js:527:11)在ServerResponse.setHeader(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:50:20) at接下来(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:162:13)在下一个(/ home / eugene / public_html / all_things_node / projects / fb2 / node_modules /connect/lib/http.js:195:11)在下一个(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:150:23)在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 (/ home / eugene / public_html / all_things_node / projects / fb2 / node_modules / connect / lib)在/ home / 在Object.auth [as handle](/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/index.js:153:7)错误:可以' t发送后设置标题。 在ServerResponse。< anonymous> (http.js:527:11)在ServerResponse.setHeader(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:50:20) at接下来(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:162:13)在下一个(/ home / eugene / public_html / all_things_node / projects / fb2 / node_modules /connect/lib/http.js:207:9)在下一个(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:150:23)在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 (/ home / eugene / public_html / all_things_node / projects / fb2 / node_modules / connect / lib)在/ home / 在Object.auth [as handle](/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/index.js:153:7)错误:可以' t发送后设置标题。 在ServerResponse。< anonymous> (http.js:527:11)在ServerResponse.setHeader(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:50:20) at接下来(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:162:13)在下一个(/ home / eugene / public_html / all_things_node / projects / fb2 / node_modules /connect/lib/http.js:150:23)在下一个(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:207:9)在Object.auth [as handle](/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect-auth/lib/index.js:153:7)在下一个(/ home / eugene /public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:198:15)在HTTPServer.handle(/ home / eugene / public_html / all_things_node / projects / fb2 / node_modules / connect / lib /http.js:211:3)在Object.handle(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:105:14)在NEX t(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:198:15)错误:发送后不能设置标题。 在ServerResponse。< anonymous> (http.js:527:11)在ServerResponse.setHeader(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:50:20) at接下来(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:162:13)在下一个(/ home / eugene / public_html / all_things_node / projects / fb2 / node_modules /connect/lib/http.js:150:23)在下一个(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:207:9)在HTTPServer.handle(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:211:3)在Object.handle(/ home / eugene / public_html / all_things_node /projects/fb2/node_modules/connect/lib/http.js:105:14)在下一个(/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_h tml / all_things_node / projects / fb2 / node_modules / connect / lib / middleware / session.js:338:9 node.js:134 throw e; // process.nextTick错误,或第一个tick'$'错误'事件 ^ 错误:发送后无法设置头文件。 在ServerResponse。< anonymous> (http.js:527:11)在ServerResponse.setHeader(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/patch.js:50:20) at接下来(/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/http.js:162:13)在下一个(/ 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 在/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/session.js:338:9 在Array。< anonymous> (/home/eugene/public_html/all_things_node/projects/fb2/node_modules/connect/lib/middleware/session/memory.js:57:7)在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; //输入安全性的随机哈希 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); 我可以知道我的代码有什么问题吗? / p> 我真的很喜欢这一点,所以很抱歉,只要把代码放在这里。 $ b $在Express中的 res 对象是解决方案 .org / docs / latest / api / http.html#http.ServerResponserel =noreferrer> 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 是 null )。并且由于错误发生在您实际的函数(req,res,next)(不在回调内)中,Connect可以捕获它,然后尝试发送500错误页。但是由于头文件已经发送,Node.js的 setHeader 抛出了您看到的错误。 Node.js / Express响应方式以及何时必须调用: 响应必须在头中,并保留在头: res.writeContinue() res.statusCode = 404 res.setHeader(名称,值) res.getHeader(name) res.removeHeader(name) res.header(key [,val]) (仅限Express) res.charset ='utf- 8' (仅限Express;仅影响Express特定方法) res.contentType(type) (仅限Express) 回应必须在头中,成为正文: res.writeHead(statusCode,[reasonPhrase],[headers]) 响应可以在头/身体中保留, strong>正文: res.write(chunk,encoding ='utf8') 响应可以在头/身体中成为完成: res.end([data],[encoding]) 响应可以在头/身体中,并保持当前状态: res.addTrailers(headers) 响应必须在头中,成为已完成 : return next([err])(仅限连接/ / li> 中间件中的任何例外 function(req,res,next)(仅限Connect / Express) res.send(body | status [,headers |状态[,状态]]) (仅限Express) res.attachment(filename) (仅限Express) res.sendfile(path [,options [,callback]]) (仅限Express) res.json(obj [,headers | status [,status]]) (仅限Express) res.redirect(url [,status]) (仅限Express) res.cookie(name,val [,options]) (仅限Express) res.clearCookie(name [,options]) (仅限Express) res.render(view [,options [,fn]]) (仅限Express) res.partial(view [,options]) (仅限Express) I'm fairly new to Node.js and I am having some issues.I am using Node.js 4.10 and Express 2.4.3.When I try to access http://127.0.0.1:8888/auth/facebook, i'll be redirected to http://127.0.0.1:8888/auth/facebook_callback.I then received the following error: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:9node.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)The following is my code: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 securityvar 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);May I know what is wrong with my code?I'm really new to this, so sorry for just putting up the code here.Thank you all in advance. 解决方案 The res object in Express is a subclass of Node.js's http.ServerResponse (read the http.js source). You are allowed to call res.setHeader(name, value) as often as you want until you call res.writeHead(statusCode). After writeHead, the headers are baked in and you can only call res.write(data), and finally res.end(data).The error "Error: Can't set headers after they are sent." means that you're already in the Body or Finished state, but some function tried to set a header or statusCode. When you see this error, try to look for anything that tries to send a header after some of the body has already been written. For example, look for callbacks that are accidentally called twice, or any error that happens after the body is sent.In your case, you called res.redirect(), which caused the response to become Finished. Then your code threw an error (res.req is null). and since the error happened within your actual function(req, res, next) (not within a callback), Connect was able to catch it and then tried to send a 500 error page. But since the headers were already sent, Node.js's setHeader threw the error that you saw.Comprehensive list of Node.js/Express response methods and when they must be called:Response must be in Head and remains in Head:res.writeContinue()res.statusCode = 404res.setHeader(name, value)res.getHeader(name)res.removeHeader(name)res.header(key[, val]) (Express only)res.charset = 'utf-8' (Express only; only affects Express-specific methods)res.contentType(type) (Express only)Response must be in Head and becomes Body:res.writeHead(statusCode, [reasonPhrase], [headers])Response can be in either Head/Body and remains in Body:res.write(chunk, encoding='utf8')Response can be in either Head/Body and becomes Finished:res.end([data], [encoding])Response can be in either Head/Body and remains in its current state:res.addTrailers(headers)Response must be in Head and becomes Finished:return next([err]) (Connect/Express only)Any exceptions within middleware function(req, res, next) (Connect/Express only)res.send(body|status[, headers|status[, status]]) (Express only)res.attachment(filename) (Express only)res.sendfile(path[, options[, callback]]) (Express only)res.json(obj[, headers|status[, status]]) (Express only)res.redirect(url[, status]) (Express only)res.cookie(name, val[, options]) (Express only)res.clearCookie(name[, options]) (Express only)res.render(view[, options[, fn]]) (Express only)res.partial(view[, options]) (Express only) 这篇关于错误:发送到客户端后,不能设置头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-19 20:10