您也可以尝试
let cors = require("cors");
app.use(cors(),function(req,res,next) {
res.header("Access-Control-Allow-Origin","http://localhost:4200"); // update to match the domain you will make the request from
res.header(
"Access-Control-Allow-Headers","Origin,X-Requested-With,Content-Type,Accept"
);
next();
});
,
// Enable CORS
app.use((req,next) => {
// This is how you would enable for ALL incoming requests - I don't recommend
// res.header("Access-Control-Allow-Origin","*");
// Allow multiple [specific] origins - Do it like this
const allowedOrigins = ["https://my-production-app.com","http://localhost:4200"];
const origin = req.headers.origin;
if (allowedOrigins.indexOf(origin) > -1) {
res.setHeader("Access-Control-Allow-Origin",origin);
}
res.header(
"Access-Control-Allow-Headers",Accept"
);
next();
});
本文链接:https://www.f2er.com/3162383.html