最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Node JS,express

IT培训 admin 3浏览 0评论

Node JS,express

我使用的应用程序已经具有自己的基础架构。任务是防止用户在多个浏览器中登录。我们的应用程序具有单一应用程序体系结构,因此理想情况下,用户应仅在一个浏览器选项卡中工作。我有一个问题。我无法从客户端删除Cookie。

I。简要地。

应用设置:

服务器:NodeJS端口:8083

客户:VueJS端口:8088

我使用模块express-session初始化服务器端的会话机制,并将cookie发送给客户端。客户尚未设置Cookie。

II。详细信息:

服务器的根文件是index.js

我在其中执行以下操作:

  1. 插入express模块:
const express = require('express')
  1. 插入cors模块:
const cors = require('cors')
  1. 添加cors设置:
app.use(cors({
    origin: 'http://localhost:8088',
    credentials: true
}))

然后我在user.js文件中初始化会话并接收客户端的连接:

  1. 插入express-session模块:
const session = require('express-session')
  1. 通过express.Router()插入路由:
const router = express.Router()
  1. 添加会话设置:
const EIGHT_HOURS  = 1000 * 60 * 60 * 8
const {
    SESS_NAME = 'sid',
    SESS_LIFETIME = EIGHT_HOURS,
    SESS_SECRET = 'test',
    NODE_ENV = 'development'
} = process.env
const IN_PROD = NODE_ENV === 'production'
  1. 初始化会话:
router.use(session({
    name: SESS_NAME,
    resave: false,
    saveUninitialized: false,
    secret: SESS_SECRET,
    cookie: {
        maxAge: SESS_LIFETIME,
        sameSite: false,
        // Must have HTTPS to work 'secret:true'
        secure: IN_PROD
    }
}))
  1. 通过router.post()接收客户端查询

所以我做了:

  1. 我使用req.session.destroy删除会话数据,并希望从某些浏览器中注销浏览器用户并清除cookie。
req.session.destroy(err => {
            if (err) {
                return res.send({ error: 'Logout error' })
            }
            res.clearCookie(SESS_NAME, {path: '/'})
            return res.send({ 'clearSession': 'success' })
        })

不幸的是没有魔法发生

  1. 我阅读了不同的主题。例如,此处(GitHub)提供了结论:如上所示,在res.clearCookie方法中使用显式cookie的路径指示。那没用。

  2. 在Cookie设置中写入此设置{path: '/'}。也没用。

router.use(session({
    name: SESS_NAME,
    resave: false,
    saveUninitialized: false,
    secret: SESS_SECRET,
    cookie: {
    path: '/',
        maxAge: SESS_LIFETIME,
        sameSite: false,
        // Must have HTTPS to work 'secret:true'
        secure: IN_PROD
    }
}))

并且如快速会话文档(NPM:express-session)所写,此路径是cookie存储的默认路径。

  1. 在req.session.destroy中添加req.session = null
req.session.destroy(err => {
            if (err) {
                return res.send({ error: 'Logout error' })
            }
            req.session = null
            res.clearCookie(SESS_NAME, {path: '/'})
            return res.send({ 'clearSession': 'success' })
        })

那没用

  1. delete req.session也不起作用。

所以,如何解决此问题?我该怎么办?

回答如下:

您是否尝试过通过将其设置为null来删除确切的cookie,这意味着您正在处理名为Views的cookie,可以使用req.session.Views = null删除该cookie。

Node JS,express

我使用的应用程序已经具有自己的基础架构。任务是防止用户在多个浏览器中登录。我们的应用程序具有单一应用程序体系结构,因此理想情况下,用户应仅在一个浏览器选项卡中工作。我有一个问题。我无法从客户端删除Cookie。

I。简要地。

应用设置:

服务器:NodeJS端口:8083

客户:VueJS端口:8088

我使用模块express-session初始化服务器端的会话机制,并将cookie发送给客户端。客户尚未设置Cookie。

II。详细信息:

服务器的根文件是index.js

我在其中执行以下操作:

  1. 插入express模块:
const express = require('express')
  1. 插入cors模块:
const cors = require('cors')
  1. 添加cors设置:
app.use(cors({
    origin: 'http://localhost:8088',
    credentials: true
}))

然后我在user.js文件中初始化会话并接收客户端的连接:

  1. 插入express-session模块:
const session = require('express-session')
  1. 通过express.Router()插入路由:
const router = express.Router()
  1. 添加会话设置:
const EIGHT_HOURS  = 1000 * 60 * 60 * 8
const {
    SESS_NAME = 'sid',
    SESS_LIFETIME = EIGHT_HOURS,
    SESS_SECRET = 'test',
    NODE_ENV = 'development'
} = process.env
const IN_PROD = NODE_ENV === 'production'
  1. 初始化会话:
router.use(session({
    name: SESS_NAME,
    resave: false,
    saveUninitialized: false,
    secret: SESS_SECRET,
    cookie: {
        maxAge: SESS_LIFETIME,
        sameSite: false,
        // Must have HTTPS to work 'secret:true'
        secure: IN_PROD
    }
}))
  1. 通过router.post()接收客户端查询

所以我做了:

  1. 我使用req.session.destroy删除会话数据,并希望从某些浏览器中注销浏览器用户并清除cookie。
req.session.destroy(err => {
            if (err) {
                return res.send({ error: 'Logout error' })
            }
            res.clearCookie(SESS_NAME, {path: '/'})
            return res.send({ 'clearSession': 'success' })
        })

不幸的是没有魔法发生

  1. 我阅读了不同的主题。例如,此处(GitHub)提供了结论:如上所示,在res.clearCookie方法中使用显式cookie的路径指示。那没用。

  2. 在Cookie设置中写入此设置{path: '/'}。也没用。

router.use(session({
    name: SESS_NAME,
    resave: false,
    saveUninitialized: false,
    secret: SESS_SECRET,
    cookie: {
    path: '/',
        maxAge: SESS_LIFETIME,
        sameSite: false,
        // Must have HTTPS to work 'secret:true'
        secure: IN_PROD
    }
}))

并且如快速会话文档(NPM:express-session)所写,此路径是cookie存储的默认路径。

  1. 在req.session.destroy中添加req.session = null
req.session.destroy(err => {
            if (err) {
                return res.send({ error: 'Logout error' })
            }
            req.session = null
            res.clearCookie(SESS_NAME, {path: '/'})
            return res.send({ 'clearSession': 'success' })
        })

那没用

  1. delete req.session也不起作用。

所以,如何解决此问题?我该怎么办?

回答如下:

您是否尝试过通过将其设置为null来删除确切的cookie,这意味着您正在处理名为Views的cookie,可以使用req.session.Views = null删除该cookie。

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论