0

Heroku is showing application error when deploying from github repository , Here is package.json

{
  "name": "tinder-backend",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "mongoose": "^5.11.15"
  }
}

But it always shows Application error on heroku I added gitignore to excude node_modules Here is my code (not mine but learning express from youtube --> server.js) :

import express from 'express'
import mongoose from 'mongoose'

import Cors from 'cors'
import Cards from './dbCards.js'

// App Config
const app = express();
const port = process.env.port || 8001

const connection_url = `mongodb://admin:<hidden>@cluster0-shard-00-00.7vrlv.mongodb.net:27017,cluster0-shard-00-01.7vrlv.mongodb.net:27017,cluster0-shard-00-02.7vrlv.mongodb.net:27017/<hidden>?ssl=true&replicaSet=atlas-p4781z-shard-0&authSource=admin&retryWrites=true&w=majority`
//Middlewares
app.use(express.json())
app.use(Cors())
//DB config
mongoose.connect(connection_url,{
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true,
})
//api ENDPOINTS
app.get('/',(req,res)=>{
    res.status(200).send('Hello world')
});

app.post('/tinder/card',(req,res) => {
    const dbCard = req.body;
    Cards.create(dbCard,(err,data) => {
        if(err){
            res.status(500).send(err)
        }
        else{
            res.status(201).send(data)
        }

    })
});

app.get('/tinder/card',(req,res) => {
    const dbCard = req.body;
    Cards.find((err,data) => {
        if(err){
            res.status(500).send(err)
        }
        else{
            res.status(200).send(data)
        }

    })
});
//listener
app.listen(port,()=> console.log(`listening on localhost:${port}`));

This works perfectly on local machine , beginner here any help will be appreciated

Here is the log which i see inside activity :

-----> Building on the Heroku-20 stack
-----> Node.js app detected
       
-----> Creating runtime environment
       
       NPM_CONFIG_LOGLEVEL=error
       NODE_VERBOSE=false
       NODE_ENV=production
       NODE_MODULES_CACHE=true
       
-----> Installing binaries
       engines.node (package.json):  unspecified
       engines.npm (package.json):   unspecified (use default)
       
       Resolving node version 14.x...
       Downloading and installing node 14.15.4...
       Using default npm version: 6.14.10
       
-----> Restoring cache
       - node_modules
       
-----> Installing dependencies
       Installing node modules
       added 83 packages in 2.08s
       
-----> Build
       
-----> Caching build
       - node_modules
       
-----> Pruning devDependencies
       audited 83 packages in 1.059s
       
       2 packages are looking for funding
         run `npm fund` for details
       
       found 0 vulnerabilities
       
       
-----> Build succeeded!
-----> Discovering process types
       Procfile declares types     -> (none)
       Default types for buildpack -> web
-----> Compressing...
       Done: 34.2M
-----> Launching...
       Released v6
3
  • Can you share the error message from the logs? Commented Feb 6, 2021 at 4:40
  • @ArunKumarMohan I dont use heroku cli , ok I attached Commented Feb 6, 2021 at 4:41
  • Just curious, why not? Once you have the Heroku CLI installed, you can use heroku logs -t to tail the logs. If you don't want to install it, you can view the logs from the Heroku web app. Commented Feb 6, 2021 at 4:44

2 Answers 2

1

If you properly follow the Heroku deploy guide from the Haruko website then you have to ensure you have added an IP address as a whitelist to your MongoDB account. Sometimes people face the problem with IP address, Cause when you don't add the IP address to the MongoDB account then Heroku unable to start the server. And also make sure that You added the Heroku procfile in your project root directory.

Sign up to request clarification or add additional context in comments.

5 Comments

0.0.0.0/0 is my ip in mongodb settings
how to add heroku procfile
Just create a file as the name of Procfile in your project root directory. then apply this content in your created Procfile (web: node "YOUR INDEX PAGE NAME") for example "web: node app.js". and finally You should set the engines in your Package.json file. "engines": { "node": "Your current node version" }
Okay, you're welcome. And after the try let me know what happened with you.
Yes, This is both same. You can also use the npm command in your Procfile.
1

My error was this line const port = process.env.port || 8001 , it should be process.env.PORT , port in uppercase , successfully wasted my time

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.