Node.js 从本地使用身份验证api时出现Unexpect CORS策略错误

Node.js 从本地使用身份验证api时出现Unexpect CORS策略错误,node.js,Node.js,首先,我在云上的ubuntu服务器上运行了一个后端RESTapi(100%完美运行) 我还有一个在同一台服务器上运行的前端Web应用程序(工作也很好),我可以使用登录系统和所有这些东西 现在我的问题是:出于开发目的,我正在我的计算机上本地部署前端,但我仍然希望在云上连接到我的后端,而不是在本地部署后端 现在的问题是,当我从本地计算机使用身份验证时,我遇到以下错误: Access to fetch at 'http://ip-adress-to-the-backend-at-the-cloud:5

首先,我在云上的ubuntu服务器上运行了一个后端RESTapi(100%完美运行)

我还有一个在同一台服务器上运行的前端Web应用程序(工作也很好),我可以使用登录系统和所有这些东西

现在我的问题是:出于开发目的,我正在我的计算机上本地部署前端,但我仍然希望在云上连接到我的后端,而不是在本地部署后端

现在的问题是,当我从本地计算机使用身份验证时,我遇到以下错误:

Access to fetch at 'http://ip-adress-to-the-backend-at-the-cloud:5000/api/auth' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value 'http://*:3000' that is not equal to the supplied origin. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
问题是:直到上周,在我的电脑上本地使用webapp登录时,它工作得非常好。。现在,我在“.env”文件、“server.js”和“routes.js”文件中做了一些更改,但它不起作用

奇怪的是,我试图删除这些文件中的更改,但仍然不起作用

这是我的.env文件:

 APP_PORT=*****
DB_HOST=*****
DB_USER=*****
DB_PASSWORD=******
DB_NAME=******
JWT_SECRET=******
JWT_KEYSECRET=*******
ALLOWED_ORIGIN=http://ipadress_to_both_the_frontend_and_backend:3000
这是我的server.js:

const express = require('express'); 
const app = express();
require('dotenv').config();

if(process.env.JWT_SECRET == undefined || process.env.JWT_KEYSECRET == undefined){
    console.log("Fatal error: Secret is not set")
    process.exit(1);
}


require('./startup/routes')(app)
require('./startup/database')

app.listen(process.env.APP_PORT, () => {
    console.log("Server is running: " + process.env.APP_PORT);
})
和my routes.js:

require('dotenv').config(); 
const users = require('../api/user/user.router')
const keys = require('../api/keys/keys.router')
const equipmentType = require('../api/equipmenttypes/equipmenttype.router');


const auth = require('../api/authentication/auth.router')
const helmet = require('helmet')
const express = require('express')

module.exports = function (app) {
    app.use(express.json());
    app.use(express.urlencoded({ extended: true }));
    app.use(express.static('public'))

    app.use(helmet.xssFilter())
    app.use(helmet.frameguard())
    app.use((req, res, next) => {
        res.setHeader('Access-Control-Allow-Origin', process.env.ALLOWED_ORIGIN);
        res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
        res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, x-auth-token, x-api-key');
        res.setHeader('Access-Control-Allow-Credentials', true);
        next();
    })

    app.use('/api/user', users)
    app.use('/api/auth', auth)
    app.use('/api/key', keys)
    app.use('/api/equipmenttypes', equipmentType)

}

首先,通过
npm i cors

将此添加到server.js文件:

const cors=要求(“cors”)

应用程序使用(cors())


解决方案很简单,我在我的环境文件中更改了以下行:

允许的原产地=*


不,这不是一件好事,也不安全,但现在我可以从localhost访问后端api,并且可以优化我的工作。

为什么我觉得这根本不起作用?本地pc的连接仍然被阻塞。您尝试过这样做吗?杰普,问题仍然存在。我无法从本地计算机访问backendAPI。有一个名为反向代理的东西,您可能需要查看它。我在.env文件中将行更改为:ALLOWED_ORIGIN=*非常有效。