无法读取未定义的属性“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 (将#修改为@)