gpt4 book ai didi

node.js - 在heroku上部署后无法读取mongo数据库,返回503错误,但在本地可以工作

转载 作者:太空宇宙 更新时间:2023-11-04 01:28:23 34 4
gpt4 key购买 nike

执行 npm run build 并修复了 server.js 上的路径错误后,它在本地主机中运行良好。但是,一旦我将其部署到 Heroku,数据就不再收费,并最终在 chrome 控制台中显示以下消息:

Failed to load resource: the server responded with a status of 503 (Service Unavailable)

接下来是另一个错误:

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0.

我猜第一个错误与路由有关,因为它试图获取数据并且服务器响应是 503,我猜第二个错误仍然与我的 server.js 文件上静态文件的使用路径有关。

代码可在此处获取:https://github.com/pepdbm7/Meetup-with-Vue

对于第一个错误,我已经尝试过的是:更改客户端中的路由(将 herokuapp URL 添加为每个 fetch() 的根 URL),以及服务器端路由,但没有帮助。

它在本地运行得很好,就像现在一样,这就是我不明白的地方。

...对于第二个错误,我将路径更改为: res.sendFile(path.join(__dirname, "client/dist")) 和: app.use(express.static("client/dist"));,并且我已经尝试过其他方法,但它根本不起作用,因为它无法访问缩小的 js 和 CSS 文件。

作为附加信息,我在部署时确实控制了heroku日志,这是结果日志,我没有看到主要错误,而只是警告,也许我在这里遗漏了一些东西:

2019-06-28T15:32:25.610341+00:00 app[api]: Deploy d1938a6e by user pepdbm7@gmail.com
2019-06-28T15:32:25.610341+00:00 app[api]: Release v17 created by user pepdbm7@gmail.com
2019-06-28T15:32:26.000000+00:00 app[api]: Build succeeded
2019-06-28T15:32:27.448688+00:00 heroku[web.1]: Restarting
2019-06-28T15:32:27.765993+00:00 heroku[web.1]: State changed from up to starting
2019-06-28T15:32:28.597600+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2019-06-28T15:32:28.706141+00:00 heroku[web.1]: Process exited with status 143
2019-06-28T15:32:32.750630+00:00 heroku[web.1]: Starting process with command `npm start`
2019-06-28T15:32:34.977483+00:00 app[web.1]:
2019-06-28T15:32:34.977500+00:00 app[web.1]: > meetup_app_vue@1.0.0 start /app
2019-06-28T15:32:34.977501+00:00 app[web.1]: > nodemon server.js
2019-06-28T15:32:34.977502+00:00 app[web.1]:
2019-06-28T15:32:35.398126+00:00 app[web.1]: [33m[nodemon] 1.19.1[39m
2019-06-28T15:32:35.399744+00:00 app[web.1]: [33m[nodemon] to restart at any time, enter `rs`[39m
2019-06-28T15:32:35.400250+00:00 app[web.1]: [33m[nodemon] watching: *.*[39m
2019-06-28T15:32:35.401055+00:00 app[web.1]: [32m[nodemon] starting `node server.js`[39m
2019-06-28T15:32:36.388146+00:00 heroku[web.1]: State changed from starting to up
2019-06-28T15:32:35.987818+00:00 app[web.1]: node environment is production
2019-06-28T15:32:35.993171+00:00 app[web.1]: meetup_app_vue 1.0.0 up and running on port 40353
2019-06-28T15:32:35.994993+00:00 app[web.1]: (node:36) UnhandledPromiseRejectionWarning: MongoParseError: Invalid connection string
2019-06-28T15:32:35.994996+00:00 app[web.1]: at parseConnectionString (/app/node_modules/mongodb-core/lib/uri_parser.js:505:21)
2019-06-28T15:32:35.994998+00:00 app[web.1]: at connect (/app/node_modules/mongodb/lib/operations/mongo_client_ops.js:195:3)
2019-06-28T15:32:35.994999+00:00 app[web.1]: at connectOp (/app/node_modules/mongodb/lib/operations/mongo_client_ops.js:284:3)
2019-06-28T15:32:35.995001+00:00 app[web.1]: at executeOperation (/app/node_modules/mongodb/lib/utils.js:416:24)
2019-06-28T15:32:35.995002+00:00 app[web.1]: at MongoClient.connect (/app/node_modules/mongodb/lib/mongo_client.js:175:10)
2019-06-28T15:32:35.995004+00:00 app[web.1]: at Promise (/app/node_modules/mongoose/lib/connection.js:554:12)
2019-06-28T15:32:35.995006+00:00 app[web.1]: at new Promise (<anonymous>)
2019-06-28T15:32:35.995007+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:551:19)
2019-06-28T15:32:35.995009+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:289:15)
2019-06-28T15:32:35.995010+00:00 app[web.1]: at Object.<anonymous> (/app/server.js:23:4)
2019-06-28T15:32:35.995012+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:776:30)
2019-06-28T15:32:35.995013+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
2019-06-28T15:32:35.995015+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:653:32)
2019-06-28T15:32:35.995016+00:00 app[web.1]: at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
2019-06-28T15:32:35.995017+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:585:3)
2019-06-28T15:32:35.995019+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
2019-06-28T15:32:35.995111+00:00 app[web.1]: (node:36) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
2019-06-28T15:32:35.995194+00:00 app[web.1]: (node:36) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

无论它是什么,它都必须是一个非常简单的事情,但我已经花了几天时间找到它,所以任何线索都将非常受欢迎!

编辑:正如一些评论所说,我必须将环境变量放在heroku上,我只是错过了或者我没有正确命名。谢谢!

最佳答案

正如我所料,这一定是一个简单解决方案的问题,事实也确实如此。我只需更新 Heroku 设置中的 Config Vars 中的环境变量,正如一些评论所说!现在工作正常!!

非常感谢!

关于node.js - 在heroku上部署后无法读取mongo数据库,返回503错误,但在本地可以工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56809974/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com