DEV Community

Venkat
Venkat

Posted on

Is there a way to reslove the build issue while deploying server(nodejs and mongodb) application to heroku

I created a server using Node, Express, and MongoDB Atlas.When i deploy in Heroku backend module is crashing.

2023-03-13T01:20:41.570961+00:00 heroku\[web.1\]: State changed from crashed to starting
2023-03-13T01:20:42.000000+00:00 app\[api\]: Build succeeded
2023-03-13T01:20:44.228308+00:00 heroku\[web.1\]: Starting process with command `npm start`
2023-03-13T01:20:46.233404+00:00 app\[web.1\]
2023-03-13T01:20:46.233437+00:00 app\[web.1\]: \> server@1.0.0 start
2023-03-13T01:20:46.233438+00:00 app\[web.1\]: \> node src/index.js

Here it's index.js

// index.js (server)
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const app = express()
const path = require('path')
const apiPort = 3001
const userRouter = require('./route/user')
const recipesRouter = require('./route/recipes')
require('dotenv').config()

const mongoose = require('mongoose')
const db = mongoose.connection

mongoose.connect("mongodb+srv://venkat:<>@recipecluster1.txqo0vw.mongodb.net/recipes?retryWrites=true&w=majority")

app.use(bodyParser.urlencoded({ extended: true }))
app.use(cors())
app.use(bodyParser.json())
app.use("/auth", userRouter)
app.use("/recipes", recipesRouter)

// app.use(express.static(path.join(__dirname, "../../client/build")))

// app.get("*", (res, req) => {
//     res.sendFile(path.join(__dirname, "../../client/build/index.html"))
// })

app.get('/', (req, res) => {
    res.send('Server Started!')
})

//app.listen(apiPort, () => console.log(`Server running on port ${apiPort}`))

app.listen(process.env.PORT || 3001, () => {
    console.log("Server running on port")
})
Enter fullscreen mode Exit fullscreen mode

Top comments (0)