伙计们,我一直在努力为不同的域设置 cookie。我知道这不是一个好习惯,但我只是想知道这是否可能?假设我的后端在 http://192.168.0.9:8090 上运行,而我的前端在 http://localhost:3000 上运行。现在我需要做的就是将 cookie 从我的后端设置到前端。我能做到吗?我已经尝试放置 cors 来源,但前端仍然没有收到 cookie。
启用 CORS 的后端代码:
const express = require ('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const admin = require ('./Routes/admin');
const gerai = require('./Routes/gerai');
const customer = require('./Routes/customer');
const db = require("./Connection/pg_pool");
const authentication = require("./Config/auth");
const connectionError = require("./Config/connectionError");
const cors = require('cors');
const httpProxy = require('http-proxy');
let port = 8090;
let hostname = '192.168.0.9';
const { encode } = require('node-encoder');
const axios = require('axios');
let payment = paymentConf.paymentConf;
let errorMsg = connectionError.connectionError();
let auth = authentication.auth;
const app = express();
const server = require("http").createServer(app);
app.use(express.json());
app.use(cors({origin: 'http://localhost:3000', credentials: true}));
app.use(express.urlencoded({extended: true}));
app.use(cookieParser());
app.use("/app/admin", admin);
app.use("/app/gerai", gerai);
app.use("/app/customer", customer);
server.listen(process.env.PORT || port, hostname, () => {
console.log(`Listening to ${hostname}:${port}`)
})