6-7报错 Invalid character in header content ["Set-Cookie"]

来源:7-7 session演示

幻海尘封

2019-10-07

报错:

(node:1540) UnhandledPromiseRejectionWarning: **TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["Set-Cookie"]**
    at ServerResponse.setHeader (_http_outgoing.js:473:3)
    at userResult.then.userData (C:\code\02-code\01-blog01\app.js:108:25)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:1540) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1540) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are 
not handled will terminate the Node.js process with a non-zero exit code.

代码:

//处理user 路由
        const userResult = handleUserRouter(req,res)
        if (userResult) {
            userResult.then(userData => {
                if (needSetCookie){
                    // 后端修改cookie的值
                    console.log('userId',userId,`${getCookieExpires}`)
                    res.setHeader('Set-Cookie',`userId=${userId};path=/; httpOnly;expires=${getCookieExpires}`) //限制cookie修改 只允许后端修改
                }
                res.end(
                    JSON.stringify(userData)
                )
            })
            return 
        }

log打印内容:

userId 1570447599343_0.5098702049233002 ()=>{
    const d = new Date()
    d.setTime(d.getTime()+(24*60*60*1000))
    console.log('d.toGMTString()is ',d.toGMTString())
    return d.toGMTString()
}
写回答

1回答

幻海尘封

提问者

2019-10-07

原因已找到。。。

0
2
锅里的芒果
什么原因啊?我也遇到了
2020-07-28
共2条回复

Node.js+Express+Koa2+Nest.js 开发服务端

从入门到实战,一站式掌握 Node.js+Express+Koa2

4051 学习 · 2006 问题

查看课程