简体   繁体   English

Web 应用程序上传与 nodejs 服务器错误到 Heroku

[英]Web app upload with nodejs server error to Heroku

I am trying to upload my app to Heroku which has frontEnd and sending mail backend both are working in my local machine.我正在尝试将我的应用程序上传到 Heroku,它的前端和发送邮件后端都在我的本地机器上工作。 Please help anyone to solve my issue that will be of great help because this is my first and practice project to be live.请帮助任何人解决我的问题,这将有很大帮助,因为这是我的第一个实践项目。

The structures and error are mentions below:结构和错误如下所述:

文件结构

Heroku error Heroku 错误

heroku 错误

heroku logs --tail heroku 日志 --tail

heroku日志---尾巴

client package.json客户package.json

{
  "name": "hephy-web",
  "version": "0.1.0",
  "private": true,

  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "axios": "^0.19.2",
    "bootstrap": "^4.4.1",
    "react": "^16.13.1",
    "react-bootstrap": "^1.0.0",
    "react-dom": "^16.13.1",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.4.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "heroku-postbuild": "cd client && npm install && npm run build",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

server package.json服务器 package.json

{
  "name": "hephy-back-end",
  "version": "1.0.0",
  "description": "Hephy BackEnd",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Uchom",
  "license": "ISC",
  "dependencies": {
    "@sendgrid/mail": "^7.0.1",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "express-handlebars": "^4.0.3",
    "nodemailer": "^6.4.6"
  }
}

server app.js服务器 app.js

require('dotenv').config()
const path = require('path')
var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');
var cors = require('cors');
require('dotenv').config()


var transport = {
  service: 'gmail',
  auth: {
    user: process.env.EMAIL_USER,
    pass: process.env.EMAIL_PASS
  }
}

var transporter = nodemailer.createTransport(transport)

transporter.verify((error, success) => {
  if (error) {
    console.log(error);
  } else {
    console.log('Server is ready to take messages');
  }
});

router.post('/send', (req, res, next) => {
  var name = req.body.name
  var email = req.body.email
  var message = req.body.message
  var content = `Name: ${name} \n \nE-mail: ${email} \n \nMessage: ${message} `

  var mail = {
    from: name,
    to: 'xxxxxxxxxxx',  // Change to email address that you want to receive messages on
    subject: 'Hephy Inquiry Contact',
    text: content
  }

  transporter.sendMail(mail, (err, data) => {
    if (err) {
      res.json({
        status: 'fail'
      })
    } else {
      res.json({
        status: 'success'
      })
    }
  })
})

const app = express()
app.use(cors())
app.use(express.json())
app.use('/', router)
app.listen(3005)

// Serve static files from the React frontend app
app.use(express.static(path.join(__dirname, 'client/build')))

it looks like you app listens to port 3005, this wont work on Heroku where you need to bind to the port provided看起来您的应用程序正在侦听端口 3005,这在 Heroku 上不起作用,您需要绑定到提供的端口

const PORT = process.env.PORT || 3005;

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

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