简体   繁体   English

无法在 Node.js 到 Heroku 中部署联系人页面后端

[英]Unable to deploy Contact Page Backend in Node.js to Heroku

I wrote backend in Node.js for the contact page on my portfoilio on Firebase. I'm trying to deploy it, but when open the app, it gives me an 'Application Error'.我在 Node.js 中为 Firebase 上我的 portfoilio 上的联系页面写了后端。我正在尝试部署它,但是当打开应用程序时,它给了我一个“应用程序错误”。 When I go to the logs, it gives me error code=H10 desc="App crashed" .当我 go 到日志时,它给了我error code=H10 desc="App crashed"

Update: I also see an error Error: Cannot find module '@sendGrid/mail' .更新:我还看到一个错误Error: Cannot find module '@sendGrid/mail'

I've tried a few things.我试过一些东西。 I added "start": "node App.js" and "engines": { "node": "12.13.1" } to my package.json .我将"start": "node App.js""engines": { "node": "12.13.1" }添加到我的package.json I created a Procfile with web: node App.js .我用web: node App.js In my App.js , I changed my app.listen(4000, '0.0.0.0');在我的App.js中,我更改了我的app.listen(4000, '0.0.0.0'); to app.listen(process.env.PORT || 4000); app.listen(process.env.PORT || 4000); . .

I'm not sure if I have to set process.env.PORT to something.我不确定是否必须将process.env.PORT设置为某些内容。 How would I fix this?我将如何解决这个问题?

Relevant Code相关代码

App.js应用程序.js

const express = require('express'); //Needed to launch server.
const bodyParser = require('body-parser');
const cors = require('cors'); //Needed to disable sendgrid security.
const sendGrid = require('@sendGrid/mail'); //Access SendGrid library to send emails.
sendGrid.setApiKey(process.env.SENDGRID_API_KEY);
const app = express(); //Alias from the express function.

app.use(bodyParser.json());

app.use(cors());

app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    next();
});

app.get('/api', (req, res, next) => {
    res.send('API Status: Running');
});

app.post('/api/email', (req, res, next) => {
    console.log(req.body);
    const msg = {
        to: 'my@email.com',
        from: req.body.email,
        subject: req.body.subject,
        text: req.body.message
    }
    sendGrid.send(msg)
        .then(result => {
            res.status(200).json({
                success: true
            });
        })
        .catch(err => {
            console.log('error: ', err);
            res.status(401).json({
                success: false
            });
        });
});

app.listen(process.env.PORT || 4000);

Also, here's my package.json另外,这是我的package.json

{
  "name": "my-app-api",
  "version": "1.0.0",
  "description": "",
  "main": "App.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node App.js"
  },
  "author": "Daniel Zhang",
  "license": "ISC",
  "dependencies": {
    "@sendgrid/mail": "^7.2.2",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "nodemon": "^2.0.4"
  },
  "engines": {
    "node": "12.13.1"
  },
  "devDependencies": {}
}

Also, here are the Application Logs:另外,这里是应用程序日志:

2020-08-30T14:49:33.088197+00:00 heroku[web.1]: Starting process with command `node index.js`
2020-08-30T14:49:35.380821+00:00 heroku[web.1]: Process exited with status 1
2020-08-30T14:49:35.420886+00:00 heroku[web.1]: State changed from starting to crashed
2020-08-30T14:49:35.328408+00:00 app[web.1]: internal/modules/cjs/loader.js:800
2020-08-30T14:49:35.328435+00:00 app[web.1]:     throw err;
2020-08-30T14:49:35.328436+00:00 app[web.1]:     ^
2020-08-30T14:49:35.328436+00:00 app[web.1]: 
2020-08-30T14:49:35.328437+00:00 app[web.1]: Error: Cannot find module '@sendGrid/mail'
2020-08-30T14:49:35.328437+00:00 app[web.1]: Require stack:
2020-08-30T14:49:35.328437+00:00 app[web.1]: - /app/index.js
2020-08-30T14:49:35.328438+00:00 app[web.1]:     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:797:15)
2020-08-30T14:49:35.328438+00:00 app[web.1]:     at Function.Module._load (internal/modules/cjs/loader.js:690:27)
2020-08-30T14:49:35.328439+00:00 app[web.1]:     at Module.require (internal/modules/cjs/loader.js:852:19)
2020-08-30T14:49:35.328439+00:00 app[web.1]:     at require (internal/modules/cjs/helpers.js:74:18)
2020-08-30T14:49:35.328439+00:00 app[web.1]:     at Object.<anonymous> (/app/index.js:4:18)
2020-08-30T14:49:35.328440+00:00 app[web.1]:     at Module._compile (internal/modules/cjs/loader.js:959:30)
2020-08-30T14:49:35.328440+00:00 app[web.1]:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
2020-08-30T14:49:35.328440+00:00 app[web.1]:     at Module.load (internal/modules/cjs/loader.js:815:32)
2020-08-30T14:49:35.328441+00:00 app[web.1]:     at Function.Module._load (internal/modules/cjs/loader.js:727:14)
2020-08-30T14:49:35.328441+00:00 app[web.1]:     at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10) {
2020-08-30T14:49:35.328441+00:00 app[web.1]:   code: 'MODULE_NOT_FOUND',
2020-08-30T14:49:35.328442+00:00 app[web.1]:   requireStack: [ '/app/index.js' ]
2020-08-30T14:49:35.328442+00:00 app[web.1]: }
2020-08-30T14:49:46.278674+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/api" host=daniel-zhang-portfolio-backend.herokuapp.com request_id=b50170d2-6e1f-4697-aa35-3ea445d1d936 fwd="75.75.104.235" dyno= connect= service= status=503 bytes= protocol=https
2020-08-30T14:49:46.490432+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=daniel-zhang-portfolio-backend.herokuapp.com request_id=cb1bdef2-0cb6-400d-aaf5-e8e2c2b8fa35 fwd="75.75.104.235" dyno= connect= service= status=503 bytes= protocol=https

Also here's my file structure:这也是我的文件结构:

文件结构

In const sendGrid = require('@sendGrid/mail');const sendGrid = require('@sendGrid/mail'); , '@sendGrid/mail' should be '@sendgrid/mail' . , '@sendGrid/mail'应该是'@sendgrid/mail'

I'm posting this answer because I was getting the same error message.我发布此答案是因为我收到了相同的错误消息。 However, the issue was not related to the case import/require statement.但是,该问题与 case import/require 语句无关。

The error i was facing was following (Error: Cannot find module '@sendgrid/mail')我面临的错误如下(错误:找不到模块“@sendgrid/mail”)

在此处输入图像描述

However, my application was running locally without any errors, as @sendgrid/mail was present inside my node_modules folder.但是,我的应用程序在本地运行时没有任何错误,因为 @sendgrid/mail 存在于我的 node_modules 文件夹中。

在此处输入图像描述

So what was the culprit?那么罪魁祸首是什么?

Thw issue was inside my package.json file, as it was not present inside it as a dependency. Thw 问题在我的package.json文件中,因为它不作为依赖项存在于其中。

Solution解决方案

I ran the following commands inside my Vs code terminal.我在我的 Vs 代码终端中运行了以下命令。

npm install @sendgrid/mail
git add .
git commit -m "my fix"
git push heroku master

Note: you may update the push command according to your branch, in my case it was master.注意:您可以根据您的分支更新推送命令,在我的例子中是 master。

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

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