问题
I've enabled cors on my node backend with one endpoint that doesn't return data but redirect to an external url. I'm getting Access to fetch at 'https://google.com/' (redirected from 'http://localhost:8080/not-working') from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
error message.
// my express server
const express = require('express');
const cors = require('cors');
const app = express();
// this one works, fetch('http://localhost:8080/it-works').then(...).catch(...)
// from http://localhost:3000
app.get('/it-works', cors(), (req, res, next) => {
res.json({msg: 'Works! 🎉'});
});
// this one doesn't work, fetch('http://localhost:8080/not-working').then(...).catch(...)
// from http://localhost:3000
app.get('/not-working', cors(), (req, res, next) => {
res.redirect('https://google.com');
res.end();
})
来源:https://stackoverflow.com/questions/59758422/getting-cors-error-when-redirecting-with-express-server