亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

錯誤:無法在將標頭發送到客戶端后設置標頭

錯誤:無法在將標頭發送到客戶端后設置標頭

錯誤:無法在將標頭發送到客戶端后設置標頭我是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)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']);     }我可以知道我的代碼有什么問題嗎?
查看完整描述

3 回答

?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

resExpress中的對象是Node.js的http.ServerResponse子類(讀取http.js源代碼)。您可以隨時撥打電話res.setHeader(name, value),直到您打電話為止res.writeHead(statusCode)。之后writeHead,標題被烘焙,您只能調用res.write(data),最后res.end(data)。

錯誤“錯誤:發送后無法設置標頭”。表示您已處于Body或Finished狀態,但某些函數嘗試設置標題或statusCode。當您看到此錯誤時,嘗試查找在已經寫入某些正文后嘗試發送標頭的任何內容。例如,查找意外調用兩次的回調,或發送正文后發生的任何錯誤。

在你的情況下,你打電話res.redirect(),導致響應成為完成。然后你的代碼拋出一個錯誤(res.reqnull)。并且由于錯誤發生在您的實際內function(req, res, next)(不在回調中),Connect能夠捕獲它,然后嘗試發送500錯誤頁面。但由于標題已經發送,Node.js setHeader拋出了你看到的錯誤。

Node.js / Express響應方法的完整列表以及何時必須調用它們:

回復必須在Head并保持在Head

  1. res.writeContinue()

  2. res.statusCode = 404

  3. res.setHeader(name, value)

  4. res.getHeader(name)

  5. res.removeHeader(name)

  6. res.header(key[, val]) (僅限快遞)

  7. res.charset = 'utf-8' (僅限Express;僅影響特定于Express的方法)

  8. res.contentType(type) (僅限快遞)

響應必須在Head并成為Body

  1. res.writeHead(statusCode, [reasonPhrase], [headers])

響應可以在頭部/身體中,并保留在身體中

  1. res.write(chunk, encoding='utf8')

響應可以在頭部/身體中完成并且完成

  1. res.end([data], [encoding])

響應可以在頭部/身體中,并保持其當前狀態:

  1. res.addTrailers(headers)

響應必須在Head并且已完成

  1. return next([err]) (僅限連接/快速)

  2. 中間件中的任何異常function(req, res, next)(僅限Connect / Express)

  3. res.send(body|status[, headers|status[, status]]) (僅限快遞)

  4. res.attachment(filename) (僅限快遞)

  5. res.sendfile(path[, options[, callback]]) (僅限快遞)

  6. res.json(obj[, headers|status[, status]]) (僅限快遞)

  7. res.redirect(url[, status]) (僅限快遞)

  8. res.cookie(name, val[, options]) (僅限快遞)

  9. res.clearCookie(name[, options]) (僅限快遞)

  10. res.render(view[, options[, fn]]) (僅限快遞)

  11. res.partial(view[, options]) (僅限快遞)


查看完整回答
反對 回復 2019-05-27
?
暮色呼如

TA貢獻1853條經驗 獲得超9個贊

我有同樣的問題并且意識到這是因為我在res.redirect沒有return聲明的情況下進行調用,所以next之后也立即調用了該函數:

auth.annonymousOnly = function(req, res, next) {
    if (req.user) res.redirect('/');
    next();};

應該是:

auth.annonymousOnly = function(req, res, next) {
    if (req.user) return res.redirect('/');
    next();};


查看完整回答
反對 回復 2019-05-27
  • 3 回答
  • 0 關注
  • 1686 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號