简体   繁体   English

尝试在 Heroku 上托管我的 rest api (node.js) 时如何解决此问题?

[英]How do I fix this problem when trying to host my rest api (node.js) on Heroku?

在此处输入图片说明 https://github.com/pickypickleapp/herokutest123 https://github.com/pickypickleapp/herokutest123

Hello I just started learning about Rest API's and I am trying to make my own one and host/upload it on heroku.您好,我刚刚开始学习 Rest API,我正在尝试制作自己的 API 并将其托管/上传到 heroku。 I get this error when trying to host.尝试托管时出现此错误。 I've tried many solutions and most seem to say its Procfile related but it doesn't seem to be working for me.我尝试了很多解决方案,大多数似乎都说它与 Procfile 相关,但它似乎对我不起作用。 Any suggestions?有什么建议?

If you see a little above your message, you'll probably notice you missed the dotenv/config package, referenced in server.js (line 6).如果您在消息上方看到一点,您可能会注意到您错过了 server.js 中引用的dotenv/config包(第 6 行)。 Otherwise, committing the node_module is not good practice, instead, commit the file package-lock.json .否则,提交node_module不是一个好习惯,而是提交文件package-lock.json It will ensure that the proper packages, meaning the right code according to the version, will be installed on heroku.它将确保正确的包,即根据版本的正确代码,将安装在 heroku 上。

npm install --save dotenv

First of all you need to understand what happens when you load your website to Heroku.首先,您需要了解将网站加载到 Heroku 时会发生什么。 It looks for package.json to install dependencies.它寻找 package.json 来安装依赖项。 When you run npm install on your machine, that happens only on your machine.当你在你的机器上运行npm install时,这只会发生在你的机器上。 Heroku is a different machine, so it needs to install everything too. Heroku 是一台不同的机器,所以它也需要安装所有东西。 To see how it happens do the following on your machine.要查看它是如何发生的,请在您的机器上执行以下操作。

  1. Remove node_modules rm -rf node_modules删除 node_modules rm -rf node_modules

  2. Run npm install运行npm install

  3. Go to localhost and see what happens.转到本地主机,看看会发生什么。

If something is wrong you will see an error in your node console.如果出现问题,您将在节点控制台中看到错误。

Also when you put your website on hosting service you do not need to use nodemon package, as it is used in development only.此外,当您将网站放在托管服务上时,您不需要使用nodemon包,因为它仅用于开发。 Instead of this you want to use pm2 to monitor your app performance and automatically reload in case it crashes.而不是这个,你想使用pm2来监控你的应用程序性能并在它崩溃时自动重新加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM