微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法读取未定义的属性“get” | res.cookies.get()

如何解决无法读取未定义的属性“get” | res.cookies.get()

我需要从我的网站获取 cookie,但它说 res.cookies.get() 未定义。

server.js,主代码文件

const express = require('express')
const cookies = require('cookies')
const middleware = require('./middleware')

const rootRoutes = require('./routes/root-routes')
const authRoutes = require('./routes/auth-routes')

const app = express()

app.use('/',middleware.updateUser,rootRoutes,authRoutes)

app.set('views',__dirname + '/views')

app.set('view engine','pug')

app.set(cookies.express('a','b','c'))

app.get('*',(req,res) => res.render('errors/404'))

const port = 3000

app.listen(port,() => console.log(`Server is live on port ${port}`))\

middleware.js,中间件代码

const authClient = require('./auth-client')
const cookies = require('cookies')

module.exports.updateUser = async (req,res,next) => {
  try {
    const key = res.cookies.get('key');
    if (key)
      res.locals.user = await authClient.getUser(key);
  } finally {
    next();
  }
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。