0

I am trying to upload my app to Heroku which has frontEnd and sending mail backend both are working in my local machine. 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:

file structures

Heroku error

heroku error

heroku logs --tail

heroku logs  ---tail

client 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

{
  "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

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')))
Thokchom Rajesh
  • 169
  • 1
  • 3
  • 9

1 Answers1

0

it looks like you app listens to port 3005, this wont work on Heroku where you need to bind to the port provided

const PORT = process.env.PORT || 3005;
Beppe C
  • 11,256
  • 2
  • 19
  • 41
  • thanks for your guide with your valuable time BEPPE C.................I added this "const PORT = process.env.PORT || 3005;" to server's app.js on top and called PORT to app.listen() like app.listen(PORT), but no luck..........any other suggestions, please – Thokchom Rajesh Apr 27 '20 at 02:23
  • I looked better at the logs (although the port binding is now fine) the error appears at start up (missing script start), not sure where the problem is but it looks like it is the way the app starts after deploying on Heroku. Maybe this can help https://stackoverflow.com/questions/34631300/error-when-deploying-app-to-heroku. Sorry cannot help further – Beppe C Apr 27 '20 at 18:54