CORS问题在使用Heroku的Node服务器中。

huangapple go评论58阅读模式
英文:

CORS issue in node server using heroku

问题

我已经为您翻译好了以下内容:

"我已经为几天研究了这个问题,但我不明白为什么在尝试从Heroku上的我的服务器应用程序请求信息时会出现CORS问题。请查看下面的代码以及git存储库的链接。您会看到我在依赖项下的JSON文件中有CORS包。我需要更新node.js或者其他什么吗?这是dynos的问题吗(我几乎不知道dynos是什么,以及是否需要它们用于这个简单的项目)?也许是Procfile或.env的问题?我在.gitignore中有node_modules,但Heroku会自动安装在package-lock.json中找到的依赖关系,对吗?我甚至使用了CLI npm i 来确保。我陷入困境了。任何帮助将不胜感激:"

git存储库

server.js:

// 依赖项
require("dotenv").config();
const express = require('express'); // 服务器框架
const cors = require('cors'); // 跨源资源共享,用于访问外部数据源

// JSON文件
const projects = require("./projects.json");
const about = require("./about.json");
const tutorials = require("./tutorials.json");

// 创建应用程序对象
const app = express();
const PORT = process.env.PORT || 4000;

// 中间件
app.use(cors());

// 路由

// 主页路由,用于API测试
app.get("/", (req, res) => {
    res.send("Hello World")
});

// 用于检索项目的路由
app.get("/projects", (req, res) => {
    // 以JSON格式发送项目
    res.json(projects);
});

// 用于检索关于信息的路由
app.get("/about", (req, res) => {
    // 以JSON格式发送关于信息
    res.json(about);
});

// 用于检索教程信息的路由
app.get("/tutorials", (req, res) => {
    // 以JSON格式发送教程信息
    res.json(tutorials);
});

// 监听端口
app.listen(PORT, () => console.log("正在监听端口", PORT));
英文:

I've researched this issue for days and I can't understand why I'm having this CORS issue when trying to request information from my server app on Heroku. See the code below as well as a link to the git repo. You will see I have the CORS package in JSON file under dependencies. Do I need to update node or something? Is it a dynos issue (I barely know what dynos are and if I need them for this simple project)? Maybe a Procfile or .env issue? I have node_modules in .gitignore but Heroku automotaically installs dependecies found in package-lock.json, right? I even used cli npm i just to be sure. I'm stuck. Any help will be absolutely appreciated:

git repo
server.js:

    ////////////////////
// DEPENDENCIES
///////////////////
require("dotenv").config();

const express = require('express'); //server framework
const cors = require('cors'); //cross origin resource sharing to access data from foreign origins

/////////////////////
// JSON FILES
////////////////////
const projects = require("./projects.json");
const about = require("./about.json");
const tutorials = require("./tutorials.json");

// Create application object
const app = express();
const PORT = process.env.PORT || 4000;

//////////////
// MIDDLEWARE
//////////////
app.use(cors());

//////////////
// ROUTES
//////////////

//home route for api testing
app.get("/", (req, res) =>{
    res.send("Hello World")
});

//route for retrieving projectws
app.get("/projects", (req, res)=>{
    //send projects via JSON
    res.json(projects);
});

//route for retrieving about info
app.get("/about", (req, res) =>{
    //send about via JSON
    res.json(about);
});

//route for retrieving tutorial info
app.get("/tutorials", (req, res)=>{
    //send about via JSON
    res.json(tutorials);
});

/////////////
// PORT LISTENER
////////////
app.listen(PORT, () => console.log("They're listening on port", PORT));

答案1

得分: 1

我发现了这个问题。Heroku要求订阅其网络服务,所以服务器应用从未运行。我切换到render.com,现在可以正常运行。

英文:

I found this issue. Heroku requires a subscription for its webservices, so the server application was never running. I switch to render.com and it works fine now.

huangapple
  • 本文由 发表于 2023年6月13日 08:30:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定