getting CORS error when redirecting with express server [duplicate]

a 夏天 提交于 2020-01-26 03:55:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!