Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

javascript - 错误:将标头发送到客户端后无法设置标头(Error: Can't set headers after they are sent to the client)

I'm fairly new to Node.js and I am having some issues.(我对Node.js相当陌生,遇到了一些问题。)

I am using Node.js 4.10 and Express 2.4.3.(我正在使用Node.js 4.10和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 .(当我尝试访问http://127.0.0.1:8888/auth/facebook时 ,我将重定向至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: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) 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 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); May I know what is wrong with my code?(我可以知道我的代码有什么问题吗?)   ask by DjangoRocks translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The res object in Express is a subclass of Node.js's http.ServerResponse ( read the http.js source ).(Express中的res对象是Node.js的http.ServerResponse的子类(请阅读http.js源代码 )。)

You are allowed to call res.setHeader(name, value) as often as you want until you call res.writeHead(statusCode) .(允许您res.setHeader(name, value)调用res.setHeader(name, value)直到调用res.writeHead(statusCode)为止。) After writeHead , the headers are baked in and you can only call res.write(data) , and finally res.end(data) .(在writeHead之后,头将被烘焙,您只能调用res.write(data) ,最后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.(表示您已经处于“正文”或“完成”状态,但是某些函数试图设置标头或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.(在您的情况下,您调用了res.redirect() ,这导致响应变为“完成”。) Then your code threw an error ( res.req is null ).(然后,您的代码引发了错误( res.reqnull )。) 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.(并且由于错误发生在您的实际function(req, res, next) (而不是在回调内),因此Connect能够捕获该错误,然后尝试发送500错误页面。) But since the headers were already sent, Node.js's setHeader threw the error that you saw.(但是由于标头已经发送,Node.js的setHeader引发了您看到的错误。) Comprehensive list of Node.js/Express response methods and when they must be called:(Node.js / Express响应方法的完整列表以及何时调用它们:) Response must be in Head and remains in Head :(回复必须在标题中,并保持在标题中 :) res.writeContinue() res.statusCode = 404 res.setHeader(name, value) res.getHeader(name) res.removeHeader(name) res.header(key[, val]) (Express only)(res.header(key[, val]) (仅表达)) res.charset = 'utf-8' (Express only; only affects Express-specific methods)(res.charset = 'utf-8' (仅Express;仅影响Express特定的方法)) res.contentType(type) (Express only)(res.contentType(type) (仅表达)) 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 :(响应可以是Head / Body ,也可以是Finished :) res.end([data], [encoding]) Response can be in either Head/Body and remains in its current state:(响应可以处于Head / Body ,也可以保持其当前状态:) res.addTrailers(headers) Response must be in Head and becomes Finished :(响应必须在标题中,并已完成 :) return next([err]) (Connect/Express only)(return next([err]) (仅用于Connect / Express)) Any exceptions within middleware function(req, res, next) (Connect/Express only)(中间件function(req, res, next)内的任何异常function(req, res, next) (仅用于Connect / Express)) res.send(body|status[, headers|status[, status]]) (Express only)(res.send(body|status[, headers|status[, status]]) (仅表达)) res.attachment(filename) (Express only)(res.attachment(filename) (仅表达)) res.sendfile(path[, options[, callback]]) (Express only)(res.sendfile(path[, options[, callback]]) (仅表达)) res.json(obj[, headers|status[, status]]) (Express only)(res.json(obj[, headers|status[, status]]) (仅表达)) res.redirect(url[, status]) (Express only)(res.redirect(url[, status]) (仅表达)) res.cookie(name, val[, options]) (Express only)(res.cookie(name, val[, options]) (仅表达)) res.clearCookie(name[, options]) (Express only)(res.clearCookie(name[, options]) (仅表达)) res.render(view[, options[, fn]]) (Express only)(res.render(view[, options[, fn]]) (仅表达)) res.partial(view[, options]) (Express only)(res.partial(view[, options]) (仅表达))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...