且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用node.js和Express-Session的跨域身份验证

更新时间:2023-11-30 22:19:58

只需像这样处理CORS效果,并在代码上方添加以下代码

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, authorization");
    res.header("Access-Control-Allow-Methods", "GET,POST,DELETE,PUT,OPTIONS");
    next();
});

现在,在此中间件之后,您应该将代码放在如下位置:

app.use(session({
    secret: 'secret',
    resave: true,
    // unset: 'destroy',
    domain: '.domain.com',
    saveUninitialized: false,
    cookie:  {
        // path: '/',
        domain: '.domain.com',
        maxAge: 24 * 6 * 60 * 10000
    },
    store: new MongoStore({url: config.db, autoReconnect:true})
}))