由于几天以来,我一直在尝试让我的应用程序在Heroku上正常运行,但出现了CORS问题。 我阅读了cors软件包的文档,并且我认为我做得对,但是您可以在这里看到我的后端:https://github.com/Karlus44/orientation-back (我将粘贴相关代码) 您也可以在这里找到我的前端:https://github.com/Karlus44/orientation-front 并看到当您转到我的应用程序(此处为https://orientation-front.herokuapp.com/)时,会遇到一些CORS错误。 谢谢你的帮助 这是我服务器中的代码:
const express = require('express');
const bcrypt = require('bcrypt-nodejs');
const cors = require('cors');
const multer = require('multer');
//many declarations
const app = express();
app.use(express.urlencoded({extended: false}));
app.use(express.json());
var corsOptions = {
origin: 'https://orientation-front.herokuapp.com',
methods: 'GET,POST',
allowedHeaders: 'Content-Type',
optionsSuccessStatus: 200
}
app.options('*', cors(corsOptions));
app.get('/',cors(corsOptions), (req,res)=>{
return db('utilisateurs').count('id')
.then(data => res.json(data));
})
//example of request
app.post('/signin', cors(corsOptions), (req,res) => {signin.handleSignin(req,res, db, bcrypt)})
答案 0 :(得分:0)
感谢您的尝试。 该错误实际上是在我的前台文件中的请求中。链接有误。 我只需要更改所有的“ https://git.heroku.com/orientation-back.git/” 放入“ https://orientation-back.herokuapp.com/”