const express = require('express')
const bodyParser = require('body-parser')
const nodemailer = require('nodemailer')
const path = require("path")
const cors = require('cors')
const ical = require('ical-generator')
require('dotenv').config()
const app = express()
const PORT = process.env.PORT || 3001;
const HOST = process.env.YOUR_HOST || '0.0.0.0'
app.use(express.urlencoded({extended:true}))
// app.use(bodyParser.json())
// app.use(bodyParser.urlencoded({extended:true}))
app.use(express.json());
app.use(cors())
if (process.env.NODE_ENV === "production") {
app.use("/react-portofolio/", express.static(path.join(__dirname, "client", "build")));
}
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
// app.use(express.static(path.join(__dirname, 'build')));
// app.get('/*', (req, res) => {
// res.sendFile(path.join(__dirname, 'build', 'index.html'));
// });
app.listen(PORT, HOST, () => {
console.log(`Example app listening at http://${HOST}:${ PORT }/`)
})
Sorry to bother. Similar question has been posted. However I have followed the instruction on 3 different forum but getting the same error when I push to heroku for deployment. Build succeeded but I get something already running on PORT
2020-12-08T04:17:26.884688+00:00 app[web.1]: [1] Something is already running on port 57232.
2020-12-08T04:17:27.006588+00:00 app[web.1]: [1] npm run client exited with code 0
2020-12-08T04:26:37.127662+00:00 heroku[router]: at=info method=GET path="/" host=discoversamir.herokuapp.com request_id=46d76b48-2724-4726-9db0-6a7e32aebfc3 fwd="70.119.31.93" dyno=web.1 connect=0ms service=48ms status=200 bytes=2779 protocol=https
2020-12-08T04:26:37.237894+00:00 heroku[router]: at=info method=GET path="/static/css/main.9a72c1e7.chunk.css" host=discoversamir.herokuapp.com request_id=2d0792e3-4cd4-4a05-a2a4-acc41070767d fwd="70.119.31.93" dyno=web.1 connect=0ms service=14ms status=200 bytes=2779 protocol=https
2020-12-08T04:26:37.239633+00:00 heroku[router]: at=info method=GET path="/static/js/2.2cea4fb7.chunk.js" host=discoversamir.herokuapp.com request_id=dfd7e060-3da6-4073-80cd-7499cf41456a fwd="70.119.31.93" dyno=web.1 connect=0ms service=16ms status=200 bytes=2779 protocol=https
2020-12-08T04:26:37.312361+00:00 heroku[router]: at=info method=GET path="/src/App.css" host=discoversamir.herokuapp.com request_id=739a09b6-4f5b-41e8-9c8a-fb05fd183303 fwd="70.119.31.93" dyno=web.1 connect=0ms service=13ms status=200 bytes=2779 protocol=https
2020-12-08T04:26:37.313463+00:00 heroku[router]: at=info method=GET path="/static/js/main.dff0b724.chunk.js" host=discoversamir.herokuapp.com request_id=2e3abec9-f772-4bc8-b74b-75016552c304 fwd="70.119.31.93" dyno=web.1 connect=1ms service=14ms status=200 bytes=2779 protocol=https
2020-12-08T04:26:37.399704+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=discoversamir.herokuapp.com request_id=921c7d52-76e6-4254-a9ab-cd8968dd8182 fwd="70.119.31.93" dyno=web.1 connect=0ms service=6ms status=200 bytes=2779 protocol=https
2020-12-08T04:26:37.556042+00:00 heroku[router]: at=info method=GET path="/manifest.json" host=discoversamir.herokuapp.com request_id=c58d231d-60c0-42e4-a714-30a20333a156 fwd="70.119.31.93" dyno=web.1 connect=0ms service=4ms status=200 bytes=2779 protocol=https
Here is my pacakage.JSON
"name": "react-portofolio",
"version": "1.0.0",
"description": "My portofolio",
"main": "server.js",
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"test": "echo \"Error: no test specified\" && exit 1",
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
"start:prod": "node server.js",
"start:dev": "concurrently \"nodemon --ignore 'client/*'\" \"npm run client\"",
"client": "cd client && npm run start",
"install": "cd client && npm install",
"build": "cd client && npm run build",
"heroku-postbuild": "cd client && npm install && npm run build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/agnide4/react-portofolio.git"
},
"keywords": [
"portofolio",
"resume"
],
"author": "Samir Bello",
"license": "ISC",
"bugs": {
"url": "https://github.com/agnide4/react-portofolio/issues"
},
"homepage": "https://github.com/agnide4/react-portofolio#readme",
"dependencies": {
"body-parser": "^1.19.0",
"concurrently": "^5.3.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"ical-generator": "^1.15.2",
"nodemailer": "^6.4.16",
"nodemon": "^2.0.6"
},
"devDependencies": {
"gh-pages": "^3.1.0"
},
"engines": {
"node": "12.8.0",
"npm": "6.14.9"
}
It keeps giving me a manifest.JSON error`.
repo is https://github.com/agnide4/react-portofolio . Please point me to the direction of what I am missing.
Try changing this in your server.js file
app.listen(PORT, HOST, () => {
console.log(`Example app listening at http://localhost:${PORT}`)
})
This, And also try removing the HOST declaration and process.env.HOST
app.listen(PORT, () => {
console.log(`Example app listening at http://localhost:${PORT}`)
})
Heroku assigns ports dynamically to your Node process. This should be available for use in process.env.PORT — if you want to have a default for running when none is set (ex. 8000) I'd make sure that your code is doing something like:
const port = process.env.PORT || 8000
and then passing port to your app.listen function.
Related
So I've been trying to deploy my app with Heroku, and it runs locally perfectly fine, but I keep running into the same H10 error below:
2022-09-15T02:57:14.484472+00:00 heroku[web.1]: Starting process with command `npm start`
2022-09-15T02:57:17.725863+00:00 app[web.1]:
2022-09-15T02:57:17.725906+00:00 app[web.1]: > node-sso-example-app#1.0.0 start
2022-09-15T02:57:17.725912+00:00 app[web.1]: > nodemon index.js
2022-09-15T02:57:17.725915+00:00 app[web.1]:
2022-09-15T02:57:17.731558+00:00 app[web.1]: /tmp/start-d846bb8e.sh: 1: nodemon: not found
2022-09-15T02:57:17.847830+00:00 heroku[web.1]: Process exited with status 127
2022-09-15T02:57:17.916430+00:00 heroku[web.1]: State changed from starting to crashed
2022-09-15T02:57:19.230286+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=will-dersh.herokuapp.com request_id=9e07b48e-dcd7-42eb-b6bc-d9d7e138b15a fwd="108.67.29.121" dyno= connect= service= status=503 bytes= protocol=https
2022-09-15T02:57:19.499659+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=will-dersh.herokuapp.com request_id=f7501cb9-60c7-457d-97ed-63cffdfe872f fwd="108.67.29.121" dyno= connect= service= status=503 bytes= protocol=https
Here's my index.js file:
import express from 'express'
import 'dotenv/config'
import router from './routes/index.js'
import morgan from 'morgan'
const app = express()
app.use('/public', express.static('public'))
app.use(express.urlencoded({ extended: false }))
app.use(express.json())
app.use(morgan('dev'))
app.use('/', router)
app.listen(process.env.PORT || 8000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
Here's the package.json:
{
"name": "node-sso-example-app",
"version": "1.0.0",
"description": "Example Node.js SSO App using WorkOS",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js"
},
"author": "WorkOS",
"dependencies": {
"#workos-inc/node": "^2.11.0",
"cookie-parser": "^1.4.6",
"dotenv": "^16.0.1",
"ejs": "^3.1.8",
"express": "^4.18.1",
"express-session": "^1.17.2",
"http-errors": "~1.6.3",
"morgan": "^1.10.0",
"router": "^1.3.7"
},
"devDependencies": {
"nodemon": "^2.0.19"
}
}
And if it helps, here is my Procfile:
web:node index.js
I'm not really sure where I've gone wrong so any help would be appreciated!
Please help
I'm tired of trying to do it but I can't !
After deploying to Hiruku, the home page loads, but whenever I click on another route, it shows this
Application error
Then when I restart Heroku, the error goes away, but when I hit another route, it shows Application error again and crashes the app.
Thanks in advance.
Error in Terminal
(/app/node_modules/mongodb/lib/operations/connect.js:29:9)
2022-08-19T09:21:41.269285+00:00 app[web.1]: at /app/node_modules/mongodb/lib/operations/connect.js:79:20
2022-08-19T09:21:41.269285+00:00 app[web.1]: at exitWithError (/app/node_modules/mongodb/lib/sdam/topology.js:187:53)
2022-08-19T09:21:41.269286+00:00 app[web.1]: at /app/node_modules/mongodb/lib/sdam/topology.js:191:59
2022-08-19T09:21:41.269286+00:00 app[web.1]: at /app/node_modules/mongodb/lib/sdam/topology.js:244:80
2022-08-19T09:21:41.269286+00:00 app[web.1]: at <anonymous>
2022-08-19T09:21:41.396792+00:00 heroku[web.1]: Process exited with status 1
2022-08-19T09:21:41.443509+00:00 heroku[web.1]: State changed from up to crashed
2022-08-19T09:23:05.484764+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/golpo" host=gontobyo.herokuapp.com request_id=5a8b7e57-efc9-4b9d-a069-f80cb2bf0000 fwd="27.147.190.218" dyno= connect= service= status=503 bytes= protocol=https
2022-08-19T09:23:05.709953+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=gontobyo.herokuapp.com request_id=34668197-9b3c-4bfc-af99-d287ef6c9e11 fwd="27.147.190.218" dyno= connect= service= status=503 bytes= protocol=https
2022-08-19T09:46:27.638802+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=gontobyo.herokuapp.com request_id=0956306d-eb94-49c3-ad63-843ac7d40c01 fwd="27.147.190.218" dyno= connect= service= status=503 bytes= protocol=https
2022-08-19T09:46:27.862759+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=gontobyo.herokuapp.com request_id=867140a3-8b28-414c-9505-905fc68692e3 fwd="27.147.190.218" dyno= connect= service= status=503 bytes= protocol=https
package.json {
"dependencies": {
"bcrypt": "^5.0.1",
"connect-flash": "^0.1.1",
"connect-mongodb-session": "^3.1.1",
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"ejs": "^3.1.8",
"express": "^4.18.1",
"express-session": "^1.17.3",
"express-validator": "^6.14.2",
"mongoose": "^6.5.2",
"multer": "^1.4.5-lts.1",
"nodemon": "^2.0.19"
},
"name": "project-6",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
},
"engines": {
"node": "16.16.0",
"npm": "8.11.0"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}
index.js
// External Inputs
require("dotenv").config();
const mongoose = require("mongoose");
// Internal Inputs
const app = require("./app");
// DotEnv Inputs
const PORT = process.env.PORT || 5000;
// Mongoose Connection
mongoose
.connect(process.env.DB)
.then(() => {
console.log(`DB is connected`);
})
.catch((error) => {
console.log(`DB is not connected! ${error}`);
process.exit(1);
});
// Node server
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});
app.js
// External Inputs
require("dotenv").config();
const express = require(`express`);
const cors = require(`cors`);
const app = express();
const session = require("express-session");
const MongoDBStore = require("connect-mongodb-session")(session);
const flash = require("connect-flash");
// Internal Inputs
const userRouter = require(`./routes/user.route`);
const {
bindUserWithRequest,
} = require(`./middleware/bindUserWithRequest.middleware`);
const setLocals = require(`./middleware/setLocals.middleware`);
//Mongo DB store
const store = new MongoDBStore({
uri: process.env.DB,
collection: "GontobyoSessions",
expires: 1000 * 60 * 60 * 2,
});
// Setting view engine
app.set("view engine", "ejs");
// Middlewares
app.use(cors());
app.use(express.static(`public`));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(
session({
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: false,
store: store,
})
);
app.use(flash());
app.use(bindUserWithRequest());
app.use(setLocals());
app.use(userRouter);
app.use((req, res) => {
res.render("crush");
});
// Export App
module.exports = app;
I'm working on a MERN app, pretty much done, all I have left to do is deploy it. The app works perfectly when I run it locally, but I deploy to Heroku, I get the following error in the browser console:
frozen-basin-00083.herokuapp.com/:1 Failed to load resource: the server responded with a status of 503 (Service Unavailable)
/favicon.ico:1 Failed to load resource: the server responded with a status of 503 (Service Unavailable)
Heroku logs show the following:
2021-01-12T16:35:14.000000+00:00 app[api]: Build succeeded
2021-01-12T16:35:25.312880+00:00 heroku[web.1]: Starting process with command `npm start`
2021-01-12T16:35:30.220170+00:00 app[web.1]:
2021-01-12T16:35:30.220351+00:00 app[web.1]: > budget-app#1.0.0 start /app
2021-01-12T16:35:30.220352+00:00 app[web.1]: > node server.js
2021-01-12T16:35:30.220356+00:00 app[web.1]:
2021-01-12T16:35:32.450275+00:00 app[web.1]: Server is running on port 7834
2021-01-12T16:35:32.457602+00:00 app[web.1]: Error: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
2021-01-12T16:35:32.532983+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-01-12T16:35:32.533749+00:00 app[web.1]: npm ERR! errno 1
2021-01-12T16:35:32.553311+00:00 app[web.1]: npm ERR! budget-app#1.0.0 start: `node server.js`
2021-01-12T16:35:32.553624+00:00 app[web.1]: npm ERR! Exit status 1
2021-01-12T16:35:32.553937+00:00 app[web.1]: npm ERR!
2021-01-12T16:35:32.554191+00:00 app[web.1]: npm ERR! Failed at the budget-app#1.0.0 start script.
2021-01-12T16:35:32.554488+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-01-12T16:35:33.032396+00:00 app[web.1]:
2021-01-12T16:35:33.036236+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-01-12T16:35:33.036435+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2021-01-12T16_35_32_555Z-debug.log
2021-01-12T16:35:33.119008+00:00 heroku[web.1]: Process exited with status 1
2021-01-12T16:35:33.187983+00:00 heroku[web.1]: State changed from starting to crashed
2021-01-12T16:35:37.653703+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=frozen-basin-00083.herokuapp.com request_id=8403a766-1141-47b9-8fa3-79d77bbf5d01 fwd="208.104.192.108" dyno= connect= service= status=503 bytes= protocol=https
2021-01-12T16:35:39.026779+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=frozen-basin-00083.herokuapp.com request_id=50f48bcf-4534-450d-825d-1b9e91d7ad44 fwd="208.104.192.108" dyno= connect= service= status=503 bytes= protocol=https
2021-01-12T16:38:29.709828+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=frozen-basin-00083.herokuapp.com request_id=ca7f559f-2125-48b2-a22f-9f3180acb70b fwd="208.104.192.108" dyno= connect= service= status=503 bytes= protocol=https
2021-01-12T16:38:30.395524+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=frozen-basin-00083.herokuapp.com request_id=d0937812-09be-49df-8138-564ee7bbfce0 fwd="208.104.192.108" dyno= connect= service= status=503 bytes= protocol=https
2021-01-12T16:42:35.305633+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=frozen-basin-00083.herokuapp.com request_id=3d35bf8c-5c7f-441c-817a-6e15581d3bdc fwd="208.104.192.108" dyno= connect= service= status=503 bytes= protocol=https
2021-01-12T16:42:35.923358+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=frozen-basin-00083.herokuapp.com request_id=cd050371-fb57-49b5-93f0-05a928a5fe77 fwd="208.104.192.108" dyno= connect= service= status=503 bytes= protocol=https
Initially I thought it may be a problem with favicon because of the console error. At one point I tried to use my own icon so I deleted favicon and the HTML tag for it. I saw the error, added it back in and nothing changed. I also see there's a mongoose error in the heroku logs, but locally when I run npm start, it connects the db just fine. When I run the app locally everything is exactly how it should be, I'm only getting these problems when deploying.
Here is the package.json for the server:
{
"name": "budget-app",
"version": "1.0.0",
"description": "back end for budget tool",
"main": "server.js",
"scripts": {
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
"author": "Evan Barton",
"license": "MIT",
"dependencies": {
"colors": "^1.4.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongoose": "^5.11.11",
"morgan": "^1.10.0",
"node": "^15.4.0"
},
"devDependencies": {
"concurrently": "^5.3.0",
"nodemon": "^2.0.7"
}
}
Here is the package.json for the client:
{
"name": "budget-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.11.8",
"#testing-library/react": "^11.2.3",
"#testing-library/user-event": "^12.6.0",
"axios": "^0.21.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.1",
"shortid": "^2.2.16",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:5000"
}
Here is the server.js file:
require('dotenv').config()
const path = require('path')
const express = require('express')
const colors = require('colors')
const morgan = require('morgan')
const connectDB = require('./config/db-config')
connectDB()
const app = express()
app.use(express.json())
const transactionsRouter = require('./routes/transactions-router')
app.use('/api/transactions', transactionsRouter)
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'))
})
// app.use('*', express.static(path.join(__dirname, "client", "build")))
}
const PORT = process.env.PORT || 5000
app.get('/', (req, res) => res.send('Hello'))
app.listen(PORT, console.log(`Server is running on port ${PORT}`.blue.bold))
db-config:
const mongoose = require('mongoose')
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true
})
console.log(`MongoDB connected: ${conn.connection.host}`.cyan.underline.bold)
} catch(err) {
console.log(`Error: ${err.message}`.red)
process.exit(1)
}
}
module.exports = connectDB
The issue was the environment variables. The reason it worked on my local machine was because of the env file, but when I deploy to Heroku, the env file is gitignored. So I had to set up the environment variables again in Heroku by going to setting > config vars, and configuring the variables the same way they are on my local machine. This fixed the problem instantly.
I am working on deploying Net Ninja's JWT authentication tutorial/project to heroku, but I am getting an error that I have not been able to overcome. This application works perfectly on my local computer. So far I've read through several stack overflow questions, but every fix for this issue that I've come across, I have already implemented.
Here are the heroku logs of my error.
2020-12-03T16:22:13.292858+00:00 app[web.1]: npm ERR!
2020-12-03T16:22:13.292982+00:00 app[web.1]: npm ERR! Failed at the node-express-jwt-auth#1.0.0 start script.
2020-12-03T16:22:13.293125+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-12-03T16:22:13.300898+00:00 app[web.1]:
2020-12-03T16:22:13.301049+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-12-03T16:22:13.301127+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-12-03T16_22_13_293Z-debug.log
2020-12-03T16:22:13.432936+00:00 heroku[web.1]: Process exited with status 1
2020-12-03T16:22:13.479796+00:00 heroku[web.1]: State changed from starting to crashed
2020-12-03T18:29:03.230278+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=net-ninja-jwt-tutorial.herokuapp.com request_id=b693f181-6061-4704-ad14-08b71b6b0cb0 fwd="208.102.105.218" dyno= connect= service= status=503 bytes= protocol=https
2020-12-03T18:29:04.077018+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=net-ninja-jwt-tutorial.herokuapp.com request_id=d363fc10-2e07-4e50-a024-c9f848b19c42 fwd="208.102.105.218" dyno= connect= service= status=503 bytes= protocol=https
My package.json is as follows:
{
"name": "node-express-jwt-auth",
"version": "1.0.0",
"description": "",
"main": "app.js",
"dependencies": {
"bcrypt": "^5.0.0",
"dotenv": "^8.2.0",
"ejs": "^3.1.3",
"express": "^4.17.1",
"mongoose": "^5.9.23",
"validator": "^13.1.17"
},
"devDependencies": {},
"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/iamshaunjp/node-express-jwt-auth.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/iamshaunjp/node-express-jwt-auth/issues"
},
"engine":{
"node":"12.x"
}
}
My app.js file reads as follows:
const express = require('express');
const mongoose = require('mongoose');
require('dotenv').config()
// import cookie parser
const cookieParser = require('cookie-parser');
// import require auth
const {requireAuth, checkUser} = require('./middleware/authMiddleware')
// import authRoutes
const authRoutes = require('./routes/authRoutes')
const app = express();
const port = process.env.PORT || 3000
// middleware
app.use(express.static('public'));
// below allows us to use JSON in our requests
app.use(express.json());
// allows us to use cookieParser as middleware
app.use(cookieParser());
// view engine
app.set('view engine', 'ejs');
// database connection
const dbURI = 'mongodb+srv://david:Looping!#nodeauth.uj5ph.mongodb.net/<dbname>?retryWrites=true&w=majority';
mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex:true })
.then((result) => {
app.listen(port)
console.log(`listening on port ${port}`)
})
.catch((err) => console.log(err));
// routes
//
app.get('*', checkUser);
app.get('/', (req, res) => res.render('home'));
app.get('/smoothies', requireAuth, (req, res) => res.render('smoothies'));
// allow us to import are authroutes into the app.js file.
app.use(authRoutes)
// cookies routes
// app.get('/set-cookies', (req, res) =>{
// // res.setHeader('Set-Cookie','newUser=true')
// // npm install cookie-parser
// res.cookie("newUser", false);
// res.cookie("isEmployee", true, {maxAge:1000*60*60*24, httpOnly:true});
// res.send('you got the cookies!');
// });
// app.get('/read-cookies', (req, res) =>{
// const cookies = req.cookies;
// console.log(cookies)
// res.json(cookies)
// });
Lastly, my procfile reads:
web:node app.js
If anyone were able to help me out with this, it would be a very big help.
On a side note, when my heroku logs read:
2020-12-03T16:22:13.301049+00:00 app[web.1]: npm ERR! A complete log
of this run can be found in: 2020-12-03T16:22:13.301127+00:00
app[web.1]: npm ERR!
/app/.npm/_logs/2020-12-03T16_22_13_293Z-debug.log
How would I go about accessing those error logs?
Edit: after running
$heroku logs --app net-ninja-jwt-tutorial
I disovered the following error:
2020-12-03T19:50:24.064213+00:00 app[web.1]: Error: Cannot find module 'cookie-parser'
There's a lot more to the logs, but I /think/ this is the relevant issue. However, this confuses me, I have cookie-parser in my package.json file.
I am trying to run my simple node app on Heroku.
Here is the directory structure
├── app.js
├── assets
├── blog.html
├── index.html
├── node_modules
└── package.json
Here is my app.js
let express = require('express'),
path = require('path');
var app = express();
let server = require('http').Server(app);
app.use(express.static(path.join(__dirname)));
app.get('/', function(req, res, next){
res.sendStatus(200);
});
app.get('/blog.html', function(req, res,next){
res.sendFile(path.join(__dirname+"/blog.html"));
});
app.post('/contact', function(req, res, next){
});
server.listen('8000', function() {
console.log("App is running on port 8000");
});
Here is the package.json
{
"name": "website",
"version": "1.0.0",
"engines" : {
"node" : "6.3.1",
"npm" : "3.10.3"
},
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start" : "node app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.14.0"
}
}
When I go to the console it rightly prints app is starting at xxxx port.
But then the app crashes with the following message
2016-08-10T13:12:49.839138+00:00 app[web.1]: App is running on port xxxx
2016-08-10T13:13:34.944963+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=saras-website.herokuapp.com request_id=28d8705a-d5a4-4aaa-bd8d-4c4c6101fbd4 fwd="106.51.20.181" dyno= connect= service= status=503 bytes=
2016-08-10T13:13:48.295315+00:00 heroku[web.1]: State changed from starting to crashed
2016-08-10T13:13:48.552740+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=saras-website.herokuapp.com request_id=b77e151f-7017-482d-b4ba-15d980534fd7 fwd="106.51.20.181" dyno= connect= service= status=503 bytes=
2016-08-10T13:13:50.163466+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=saras-website.herokuapp.com request_id=1e7b57e5-1056-4cb3-b41f-cd3f11794efe fwd="106.51.20.181" dyno= connect= service= status=503 bytes=
I don't know what am I doing wrong here... Help is appreciated
Set port like this
ES5:
var port = process.env.PORT || 8000;
And
server.listen(port, function() {
console.log("App is running on port " + port);
});
ES6:
const port = process.env.PORT || 8000;
And
server.listen(port, () => {
console.log("App is running on port " + port);
});
This allows Heroku to set the port at run time.