Chrome不会设置快递服务器返回的cookie。我尝试在我的localhost上执行此操作,但实际上我已经在我的Windows主机文件中将“app.com”映射到127.0.0.1。
响应标头示例:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:X-Requested-With, X-AUTHENTICATION, X-IP, Content-Type, Origin, Accept, Cookie
Access-Control-Allow-Methods:GET, POST, OPTIONS, PUT, PATCH, DELETE
Access-Control-Allow-Origin:http://app.com
Connection:keep-alive
Content-Length:0
Date:Sun, 10 Jul 2016 08:52:22 GMT
set-cookie:connect.sid=s%3AXoykypZiNMoSvH_L7nH7SqeqLydEQSHK.770Fk%2Fc4pGMu%2Bi%2BtQwQr%2BWbHkkAk%2FOFMYVlmMwzqU1o; Path=/; Expires=Mon, 11 Jul 2016 08:52:22 GMT; HttpOnly
X-Powered-By:Express
快速设置:
var path = require('path');
var bodyParser = require('body-parser');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var MongoStore = require('connect-mongo')(session);
module.exports = function (app, express, config, mongoose) {
app.use(cookieParser());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://app.com');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, X-AUTHENTICATION, X-IP, Content-Type, Origin, Accept, Cookie');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
app.use(function (req, res, next) {
console.log('coockie is:', req.cookies);
next();
});
app.use(session({
saveUninitialized: false,
resave: false,
secret: config.sessionsSecretToken,
cookie: {
secure: false,
domain: '',
path: '/',
maxAge: 1000 * 60 * 60 * 24
},
store: new MongoStore({ mongooseConnection: mongoose.connection })
}));
app.use(express.static(path.join(__dirname, '..' , 'public')));
};