我在這裏同樣的問題是我的解決方案(我保存cookies在BS和使用它的時候,它很適合我):
gulp.task('browser-sync', function() {
var cookies = {};
browserSync.init({
proxy: {
target: "localhost",
proxyReq: [
function (proxyReq) {
var cookieText = Object.keys(cookies).map(function (name) {
return name + '=' + cookies[name];
}).join('; ')
if (proxyReq._headers.cookie) {
proxyReq.setHeader('cookie', cookieText);
}
}
],
proxyRes: [
function (proxyRes, req, res) {
if (proxyRes.headers && proxyRes.headers['set-cookie']) {
proxyRes.headers['set-cookie'].forEach(function (cookie) {
var name, value;
var t = cookie.split(';')[0].split('=');
name = t[0];
value = t[1];
cookies[name] = value;
});
}
}
]
}
});
});