heroku 上传节点js项目获取错误503|| cors / header错误

xlpyo6sf  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(155)


i我试图上传我的项目(只有服务器端)客户端正在运行本地项目已上传到heroku网站,但我得到错误,我无法修复他们我使用React,节点js,表达,mongo DB

const express = require("express");
const app = express();
const express = require("express");
const cors = require("cors");

const setServerConfiguration = (app) => {
  app.use(express.urlencoded({ extended: true }));
  app.use(express.json());
  app.use(cors());
};

const mongoose = require("mongoose");
const UserRoute = require("./routes/User.route");
mongoose.connect("mongodb://localhost:27017/bank");
const setServerConfiguration = require("./config");
setServerConfiguration(app);
const port = process.env.PORT || 5000
app.use("/bank", (req,res)=>{
    res.header("Access-Control-Allow-Origin", "https://nodejs-bank.herokuapp.com/bank"); 
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
},UserRoute);

app.listen(port);


  const baseUrl = "https://nodejs-bank.herokuapp.com/bank";

  const [users, setUsers] = useState([]);
  const { t } = useTranslation();

  const fetchUsers = () => {
    axios.get(baseUrl).then((response) => {
      setUsers(response.data);
    });
  };

  useEffect(() => {
    fetchUsers();
  }, []);

lrpiutwd

lrpiutwd1#

嘿,在你的express应用程序中得到一个名为cors的包,并把它放在你的应用程序的顶部

yarn add cors
app.use(cors());

相关问题