i have been trying to deploy a Mern stack application to Heroku but prior to deploying the application after entering the following command
- git add.
- git commit -m “Final commit”
and to test the application before I finally deploy, I entered the command - Heroku local, which is supposed to run the application locally which will help to check for errors before deploying.
here’s my backend package.json config
{
"name": "Nimelssa-online-quiz",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "cd client && npm run build",
"install-client": "cd client && npm install",
"heroku-postbuild": "npm run install-client && npm run build",
"start": "node server.js",
"client": "cd client && npm start",
"dev": "concurrently -n 'server,client' -c 'green,red' \"nodemon server.js\" \"npm run client\""
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.32",
"@fortawesome/free-solid-svg-icons": "^5.15.1",
"@fortawesome/react-fontawesome": "^0.1.11",
"@hapi/joi": "^17.1.1",
"bcrypt": "^5.0.0",
"body-parser": "^1.19.0",
"concurrently": "^5.3.0",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-async-errors": "^3.1.1",
"express-session": "^1.17.1",
"helmet": "^4.1.1",
"http-errors": "^1.8.0",
"joi": "^17.2.1",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.20",
"mailgun-js": "^0.22.0",
"moment": "^2.29.1",
"mongoose": "^5.9.29",
"morgan": "^1.10.0",
"nodemailer": "^6.4.13",
"nodemailer-mailgun-transport": "^2.0.1",
"nodemailer-sendgrid-transport": "^0.2.0",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"qs": "^6.9.4"
},
"devDependencies": {
"nodemon": "^2.0.6"
},
"engines": {
"node": "12.16.1"
}
}
here’s is my server.js code
require('express-async-errors');
const morgan = require("morgan");
const helment = require("helmet");
const express = require("express");
const app = express();
const cookieParser = require("cookie-parser");
const cors = require("cors");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
require("dotenv").config();
const path = require('path');
app.use(helment());
app.use(cookieParser());
app.use(express.json());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
const dbOptions = {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
};
mongoose
.connect(process.env.MONGODB_URI || "mongodb://localhost:27017/Online-Quiz", dbOptions)
.then(() => {
console.log("Sucessfully connected to MongoDb Database");
})
.catch((err) => {
console.error("Couldnt Connect To MongoDb Database");
});
const userRouter = require("./router/User"); //check
const quizRouter = require("./router/Quiz");
const adminRouter = require("./router/Admin"); //check
app.use(morgan("tiny"));
app.use("/admin", adminRouter);
app.use("/user", userRouter);
app.use("/quiz", quizRouter);
if(process.env.NODE_ENV === 'production') {
app.use(express.static('client/build/'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Express Server is Listening on port ${PORT}`);
});
here is what is displayed on the terminal when I run Heroku local and try to access the end point HTTP://localhost:5000
$ heroku local
[WARN] ENOENT: no such file or directory, open ‘Procfile’
[OKAY] package.json file found - trying ‘npm start’
[OKAY] Loaded ENV .env File as KEY=VALUE Format
[WARN] ENOENT: no such file or directory, open ‘Procfile’
[OKAY] package.json file found - trying ‘npm start’
3:45:09 AM web.1 | > Nimelssa-online-quiz@1.0.0 start C:\Users\Marquis\Desktop\Dept-Project
3:45:09 AM web.1 | > node server.js
3:45:11 AM web.1 | Express Server is Listening on port 5000
3:45:11 AM web.1 | Sucessfully connected to MongoDb Database
3:46:03 AM web.1 | GET / 404 139 - 5.192 ms
Here is my file structure