发送标头后无法设置标头

时间:2019-09-28 17:28:09

标签: node.js express backend

我是NodeJS的新手,当我尝试使用电子邮件和密码登录时,出现以下错误: (部分)

错误:发送标头后无法设置标头。     在validateHeader(_http_outgoing.js:491:11)     在ServerResponse.setHeader(_http_outgoing.js:498:3)     在ServerResponse.header(C:\ Users \ asuss \ Desktop \ server \ node_modules \ express \ lib \ response.js:771:10)     在ServerResponse.location(C:\ Users \ asuss \ Desktop \ server \ node_modules \ express \ lib \ response.js:888:15)     在ServerResponse.redirect(C:\ Users \ asuss \ Desktop \ server \ node_modules \ express \ lib \ response.js:926:18)     在app.post(C:\ Users \ asuss \ Desktop \ server \ app.js:114:9)     在Layer.handle [作为handle_request](C:\ Users \ asuss \ Desktop \ server \ node_modules \ express \ lib \ router \ layer.js:95:5)     在下一个(C:\ Users \ asuss \ Desktop \ server \ node_modules \ express \ lib \ router \ route.js:137:13)
    在redirectHome(C:\ Users \ asuss \ Desktop \ server \ app.js:42:9)     在Layer.handle [作为handle_request](C:\ Users \ asuss \ Desktop \ server \ node_modules \ express \ lib \ router \ layer.js:95:5) 错误:发送标头后无法设置标头。     在validateHeader(_http_outgoing.js:491:11)     在ServerResponse.setHeader(_http_outgoing.js:498:3)     在ServerResponse.header(C:\ Users \ asuss \ Desktop \ server \ node_modules \ express \ lib \ response.js:771:10)     在ServerResponse.location(C:\ Users \ asuss \ Desktop \ server \ node_modules \ express \ lib \ response.js:888:15)

const express = require('express')
const session = require('express-session')
const bodyParser = require('body-parser')

const users = [
    {id: 1, name: 'Alex', email:'alex@gmail.com', password:"123"},
    {id: 2, name: 'Max', email:'max@gmail.com', password:"123"},
    {id: 3, name: 'Tiffany', email:'tiffany@gmail.com', password:"123"}
]

const app = express()

app.use(bodyParser.urlencoded({
    extended: true
}))

app.use(session({
    name: 'sid',
    resave: false,
    saveUninitialized: false,
    secret: "nikivekartal",
    cookie: {
        maxAge: 1000*60*60*2,
        sameSite: true,
    }
}))

const redirectLogin = (req, res, next) => {
    if(!req.session.userId){
        res.redirect('/login')
    }
    else {
        next()
    }
}

const redirectHome = (req, res, next) => {
    if(req.session.userId){
        res.redirect('/home')
    }
    else {
        next()
    }
}

app.get('/', (req, res) => {
    const {userId} = req.session;
    console.log("Session", req.session.userId)
    res.send(`
    <h1>Welcome!</h1>
    ${userId ? `  
    <a href='/home'>Home</a>
    <form method='post' action='/logout'>
        <button>Logout</button>
    </form>
    ` : `
    <a href='/login'>Login</a>
    <a href='/register'>Register</a>
    `}`)
})

app.get('/home', redirectLogin, (req, res) => {
    res.send(`
    <h1>Home</h1>
    <a href='/'>Main</a>
    <ul>
        <li>Apple</li>
        <li>Cherry</li>
    </ul>
    `)
})

// redirectHome mddleware ini eğer user authenticate olmuşsa login'e gitmesin
// home'a gitsin diye koyuyoruz
app.get('/login', redirectHome, (req, res) => {
    res.send(`
    <h1>Login</h1>
    <form method='post' action='/login'>
        <input type='email' name='email' placeholder='Email' required/>
        <input type='password' name='password' placeholder='Password' required/>
        <input type='submit' />
    </form>
    <a href='/register'>Register</a>
    `)
})

app.get('/register', redirectHome, (req, res) => {
    res.send(`

    <h1>Register</h1>
    <form method='post' action='/register'>
        <input name='name' placeholder='Name' required />
        <input type='email' name='email' placeholder='Email' required/>
        <input type='password' name='password' placeholder='Password' required/>
        <input type='submit' />
    </form>
    `)
})

app.post('/login', redirectHome, (req, res) => {
    const {email, password} = req.body

    if(email && password){
        const user = users.find(
            user => user.email === email && user.password === password
        )

        if(user){
            req.session.userId = user.id
            res.redirect('/home')
        }
    }

    res.redirect('/login')
})

app.post('/register', redirectHome, (req, res) => {

})

app.post('/logout', redirectLogin, (req, res) => {

})

app.listen(process.env.PORT || 3000, () => {
    console.log("Server is running");
})

我该怎么办,为什么?谢谢您的考虑。

1 个答案:

答案 0 :(得分:0)

您的/login路线可以呼叫res.redirect()两次。在其中之一后添加退货以更改此内容:

app.post('/login', redirectHome, (req, res) => {
    const {email, password} = req.body

    if(email && password){
        const user = users.find(
            user => user.email === email && user.password === password
        )

        if(user){
            req.session.userId = user.id
            res.redirect('/home')
        }
    }

    res.redirect('/login')
});

对此:

app.post('/login', redirectHome, (req, res) => {
    const {email, password} = req.body

    if(email && password){
        const user = users.find(
            user => user.email === email && user.password === password
        )

        if(user){
            req.session.userId = user.id
            res.redirect('/home')
            return;                   // <== add this return
        }
    }

    res.redirect('/login')
});