Javascript 使用Express.JS+;设置Cookie时出现问题;Nginx 我正处于从我的后端回购中解脱前端的中间。在这样做的过程中,我将我的服务器托管在我家庭网络上的个人Raspberry Pi 3上。我的前端由Netlify托管。 我遇到了一个问题,虽然我可以在Postman中看到设置的cookie,但我无法使用Express.Js在我的客户端上设置cookie
我目前的设置如下:Javascript 使用Express.JS+;设置Cookie时出现问题;Nginx 我正处于从我的后端回购中解脱前端的中间。在这样做的过程中,我将我的服务器托管在我家庭网络上的个人Raspberry Pi 3上。我的前端由Netlify托管。 我遇到了一个问题,虽然我可以在Postman中看到设置的cookie,但我无法使用Express.Js在我的客户端上设置cookie,javascript,node.js,express,nginx,express-session,Javascript,Node.js,Express,Nginx,Express Session,我目前的设置如下: FE (Netlify - example.com) -> Nginx (Reverse Proxy - api.example.com) -> Node.js (Express - listening for http) 我无法登录使用Express Session进行会话的应用程序。我可以发布登录信息,但看不到响应中设置了cookie。也就是说,我在Heroku(FE+在同一回购协议上)进行了这项工作 我尝试了很多方法来让它工作,但都失败了 我尝试过的一些解
FE (Netlify - example.com) -> Nginx (Reverse Proxy - api.example.com) -> Node.js (Express - listening for http)
我无法登录使用Express Session进行会话的应用程序。我可以发布登录信息,但看不到响应中设置了cookie。也就是说,我在Heroku(FE+在同一回购协议上)进行了这项工作
我尝试了很多方法来让它工作,但都失败了
我尝试过的一些解决方案是:
- 应用程序集('信任代理',1)
- 快速会话({proxy:true})
- 将cookie域设置为
.example.com
- 为我的域启用CORS
- 将Axios设置为使用
{withCredentials:true}
- set
proxy\u set\u头X-Forwarded-Proto-https代码>
更新:我不确定是哪一部分解决了这个问题,但这是我的工作代码 后端
app.use(
require('express-session')({
secret: process.env.MONGO_SESSIONS_SECRET,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7,
...(process.env.NODE_ENV !== 'dev'
? { domain: '.example.com' }
: {}),
},
store: store,
resave: true,
saveUninitialized: true,
})
);
app.use((req, res, next) => {
const acceptedOrigins = [
'http://example.com',
'http://beta.example.com',
'http://localhost:3000',
];
let [origin] = acceptedOrigins;
if (acceptedOrigins.includes(req.headers.origin)) origin = req.headers.origin;
res.set({
'Access-Control-Allow-Origin': origin,
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'DELETE',
});
if (process.env.NODE_ENV === 'dev' && req.method === 'OPTIONS') {
return res.sendStatus(200);
} else {
next();
}
});
import axios from 'axios';
axios.defaults.withCredentials = true;
let API_ROOT = 'http://127.0.0.1:3001/api';
export const Account = {
endpoints: {
login: `${API_ACCOUNT}/login`,
resendVerification(id) {
return `${API_ACCOUNT}/verify/resend?id=${id}`;
},
},
login(body) {
return axios.post(this.endpoints.login, body);
},
resendVerification(id) {
return axios.get(this.endpoints.resendVerification(id), {
withCredentials: true,
});
},
};
前端
app.use(
require('express-session')({
secret: process.env.MONGO_SESSIONS_SECRET,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7,
...(process.env.NODE_ENV !== 'dev'
? { domain: '.example.com' }
: {}),
},
store: store,
resave: true,
saveUninitialized: true,
})
);
app.use((req, res, next) => {
const acceptedOrigins = [
'http://example.com',
'http://beta.example.com',
'http://localhost:3000',
];
let [origin] = acceptedOrigins;
if (acceptedOrigins.includes(req.headers.origin)) origin = req.headers.origin;
res.set({
'Access-Control-Allow-Origin': origin,
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'DELETE',
});
if (process.env.NODE_ENV === 'dev' && req.method === 'OPTIONS') {
return res.sendStatus(200);
} else {
next();
}
});
import axios from 'axios';
axios.defaults.withCredentials = true;
let API_ROOT = 'http://127.0.0.1:3001/api';
export const Account = {
endpoints: {
login: `${API_ACCOUNT}/login`,
resendVerification(id) {
return `${API_ACCOUNT}/verify/resend?id=${id}`;
},
},
login(body) {
return axios.post(this.endpoints.login, body);
},
resendVerification(id) {
return axios.get(this.endpoints.resendVerification(id), {
withCredentials: true,
});
},
};
嗨,很抱歉我帮不上忙,但有同样的问题。。你设法解决了吗?我的设置与您的完全相同you@Spock我相信我确实解决了这个问题,尽管我不确定我现有代码的哪一部分修复了它。我用相关代码更改更新了原始帖子
app.use(
require('express-session')({
secret: process.env.MONGO_SESSIONS_SECRET,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7,
...(process.env.NODE_ENV !== 'dev'
? { domain: '.example.com' }
: {}),
},
store: store,
resave: true,
saveUninitialized: true,
})
);
app.use((req, res, next) => {
const acceptedOrigins = [
'http://example.com',
'http://beta.example.com',
'http://localhost:3000',
];
let [origin] = acceptedOrigins;
if (acceptedOrigins.includes(req.headers.origin)) origin = req.headers.origin;
res.set({
'Access-Control-Allow-Origin': origin,
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'DELETE',
});
if (process.env.NODE_ENV === 'dev' && req.method === 'OPTIONS') {
return res.sendStatus(200);
} else {
next();
}
});
import axios from 'axios';
axios.defaults.withCredentials = true;
let API_ROOT = 'http://127.0.0.1:3001/api';
export const Account = {
endpoints: {
login: `${API_ACCOUNT}/login`,
resendVerification(id) {
return `${API_ACCOUNT}/verify/resend?id=${id}`;
},
},
login(body) {
return axios.post(this.endpoints.login, body);
},
resendVerification(id) {
return axios.get(this.endpoints.resendVerification(id), {
withCredentials: true,
});
},
};