快速重定向错误:发送标头后无法设置标头 [英] Express redirect error: can't set headers after they are sent

查看:102
本文介绍了快速重定向错误:发送标头后无法设置标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当此代码到达重定向行时,它将引发在发送标头后无法设置标头错误"并且不进行重定向.我对未完全了解标头以及express如何与它们一起工作感到内. 有关此错误的链接使我感到困惑可能是因为我对所发生的事情没有足够的基本了解.另外,我知道这是一种幼稚的身份验证方法,但我只是在尝试使基本功能正常工作.

When this code hits the redirect line it throws the 'Can't set headers after they are sent error' and doesn't redirect. I'm guilty of not fully understanding headers and how express works with them. This link about this error is confusing me a bit, probably because I don't have a basic enough understanding of what's going on. Also, I know this is a bit of a naive approach to authenticating, but I'm just trying to get basic things to work.

    app.post('/api/login', function(req, res) {
    if (req.body.password === auth.password) {
        auth.date = new Date()
        res.redirect('/admin')
      } else {
        console.log("wrong pw")
      }
    })

更新:谢谢@Brendan Ashworth,我错过了一个显而易见的其他问题,我现在已经添加了它,但不再收到该错误.

UPDATE : thank you to @Brendan Ashworth I missed an obvious else, which I've added now and no longer get the error.

但是此行不会更改我页面的内容

However this line doesn't change the contents of my page

res.sendfile('./public/admin/views/tunes.html')

在我用auth check包裹它之前就起作用了

It worked before I wrapped it with the auth check

var auth = require('../config/auth')

module.exports = function(app) {

/*
 *  CONTENT API
 */

 //...

/*
 *  Admin Routes
 */
app.get('/admin/login', function(req, res) {
    res.sendfile('./public/admin/views/login.html')
})

app.post('/api/login', function(req, res) {
    if (req.body.password === auth.password) {
        auth.date = new Date()
        res.redirect('/admin')
    } else {
        res.json({message: 'Wrong password!'})
    }
})

app.get('/admin', function(req, res) {
    if (auth.date) {
        res.sendfile('./public/admin/views/tunes.html')
        console.log("test") // 
    } else { //added else
      res.redirect('/admin/login')
    }
})

app.get('/admin/:url', function(req, res) {
    if (auth.date) {
        res.sendfile('./public/admin/views/' + req.params.url + '.html')
    } else { //added else
      res.redirect('/admin/login')
    }
})

// frontend routes 
// route to handle all angular requests
app.get('*', function(req, res) {
    res.sendfile('./public/views/index.html') 
})

}

最终更新!!我需要做的最后一件事是在发送文件后处理重定向客户端.简单的身份验证现在可以完美地工作了!

FINAL UPDATE!! The last thing I needed was to handle the redirect client side after sending the file. Simple authentication works perfectly now!

    $http.post('/api/login', $scope.auth).success(function() {
        window.location.href = '/admin'
    })

推荐答案

错误Can't set headers after they are sent error的解释:

所有HTTP响应均遵循以下基本结构:

All HTTP responses follow this basic structure:

.. Response Line ..
.. Headers ..

.. Body ..

如果要重定向用户,首先将向Response Line发送一个重定向代码(例如300),然后向Headers发送一个Location: xxx标头.

If you want to redirect a user, first the Response Line will be sent with a redirect code (lets say 300), then the Headers will be sent with a Location: xxx header.

然后,我们终于可以发送正文(不是在重定向的情况下,而是一般而言).但是-就您的代码而言-您正在发送Body响应然后尝试重定向用户.由于标头(和响应行)都已经发送了(因为您发送了正文),因此它不能在正文之后发送更多的标头.

Then, we can finally send a body (not in the case of a redirect, but in general). However - in the case with your code - you are sending a Body response then trying to redirect the user. Since the headers (and response line) have both already been sent (because you sent the body), it can't send more headers after the body.

您的代码中的示例如下:

An example of this in your code would be:

app.get('/admin', function(req, res) {
    if (auth.date) {
        res.sendfile('./public/admin/views/tunes.html')
    }
    res.redirect('/admin/login')
})

如果我假设正确,那么您实际上想在res.sendfile()调用后单击return.如果auth.date是真实的,那么您将发送一个文件(即,正文响应),然后给出重定向代码-这是行不通的.

If I'm assuming right, you actually want to return after the res.sendfile() call. If auth.date is truthy, then you'll be sending a file (i.e. body response) and then giving a redirect code - that doesn't work.

这篇关于快速重定向错误:发送标头后无法设置标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆