簡體   English   中英

無法在 Node.js 到 Heroku 中部署聯系人頁面后端

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

我在 Node.js 中為 Firebase 上我的 portfoilio 上的聯系頁面寫了后端。我正在嘗試部署它,但是當打開應用程序時,它給了我一個“應用程序錯誤”。 當我 go 到日志時,它給了我error code=H10 desc="App crashed"

更新:我還看到一個錯誤Error: Cannot find module '@sendGrid/mail'

我試過一些東西。 我將"start": "node App.js""engines": { "node": "12.13.1" }添加到我的package.json 我用web: node App.js 在我的App.js中,我更改了我的app.listen(4000, '0.0.0.0'); app.listen(process.env.PORT || 4000); .

我不確定是否必須將process.env.PORT設置為某些內容。 我將如何解決這個問題?

相關代碼

應用程序.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);

另外,這是我的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": {}
}

另外,這里是應用程序日志:

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

這也是我的文件結構:

文件結構

const sendGrid = require('@sendGrid/mail'); , '@sendGrid/mail'應該是'@sendgrid/mail'

我發布此答案是因為我收到了相同的錯誤消息。 但是,該問題與 case import/require 語句無關。

我面臨的錯誤如下(錯誤:找不到模塊“@sendgrid/mail”)

在此處輸入圖像描述

但是,我的應用程序在本地運行時沒有任何錯誤,因為 @sendgrid/mail 存在於我的 node_modules 文件夾中。

在此處輸入圖像描述

那么罪魁禍首是什么?

Thw 問題在我的package.json文件中,因為它不作為依賴項存在於其中。

解決方案

我在我的 Vs 代碼終端中運行了以下命令。

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

注意:您可以根據您的分支更新推送命令,在我的例子中是 master。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM