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

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

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:

  1. // 依赖项
  2. require("dotenv").config();
  3. const express = require('express'); // 服务器框架
  4. const cors = require('cors'); // 跨源资源共享,用于访问外部数据源
  5. // JSON文件
  6. const projects = require("./projects.json");
  7. const about = require("./about.json");
  8. const tutorials = require("./tutorials.json");
  9. // 创建应用程序对象
  10. const app = express();
  11. const PORT = process.env.PORT || 4000;
  12. // 中间件
  13. app.use(cors());
  14. // 路由
  15. // 主页路由,用于API测试
  16. app.get("/", (req, res) => {
  17. res.send("Hello World")
  18. });
  19. // 用于检索项目的路由
  20. app.get("/projects", (req, res) => {
  21. // 以JSON格式发送项目
  22. res.json(projects);
  23. });
  24. // 用于检索关于信息的路由
  25. app.get("/about", (req, res) => {
  26. // 以JSON格式发送关于信息
  27. res.json(about);
  28. });
  29. // 用于检索教程信息的路由
  30. app.get("/tutorials", (req, res) => {
  31. // 以JSON格式发送教程信息
  32. res.json(tutorials);
  33. });
  34. // 监听端口
  35. 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:

  1. ////////////////////
  2. // DEPENDENCIES
  3. ///////////////////
  4. require("dotenv").config();
  5. const express = require('express'); //server framework
  6. const cors = require('cors'); //cross origin resource sharing to access data from foreign origins
  7. /////////////////////
  8. // JSON FILES
  9. ////////////////////
  10. const projects = require("./projects.json");
  11. const about = require("./about.json");
  12. const tutorials = require("./tutorials.json");
  13. // Create application object
  14. const app = express();
  15. const PORT = process.env.PORT || 4000;
  16. //////////////
  17. // MIDDLEWARE
  18. //////////////
  19. app.use(cors());
  20. //////////////
  21. // ROUTES
  22. //////////////
  23. //home route for api testing
  24. app.get("/", (req, res) =>{
  25. res.send("Hello World")
  26. });
  27. //route for retrieving projectws
  28. app.get("/projects", (req, res)=>{
  29. //send projects via JSON
  30. res.json(projects);
  31. });
  32. //route for retrieving about info
  33. app.get("/about", (req, res) =>{
  34. //send about via JSON
  35. res.json(about);
  36. });
  37. //route for retrieving tutorial info
  38. app.get("/tutorials", (req, res)=>{
  39. //send about via JSON
  40. res.json(tutorials);
  41. });
  42. /////////////
  43. // PORT LISTENER
  44. ////////////
  45. 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:

确定