简体   繁体   中英

Node.js Express app not working after hosting on Heroku, need help troubleshooting

My Node.js web application, built with Express, Mongoose, and EJS, is not working after hosting it on Heroku. The app connects to a MongoDB Atlas database and has routes for creating, reading, updating and deleting products. The app was working fine on localhost, but after deploying to Heroku, it is not able to connect to the database and routes are not working as expected. I am unable to find the root cause of the issue and need help troubleshooting.

const { urlencoded } = require("body-parser")
const express = require("express")
const mongoose = require('mongoose')
const app = express()
const Product = require('./models/product')
const methodOverride = require('method-override')

mongoose.connect("mongodb+srv://user:pass123@cluster0.avm9sgy.mongodb.net/?retryWrites=true&w=majority", { useNewUrlParser: true }
).then(() => console.log("Database Connection succesful")).catch(() => console.log("error"))

app.set('view engine', 'ejs')
app.use(express.urlencoded({ extended: true }))
app.use(methodOverride('_method'))

app.get('/', async (req, res) => {
    res.send('Hello')
})
app.get('/products', async (req, res) => {
    const products = await Product.find({})
    res.render('products/index', { products })
})
app.get('/products/new', async (req, res) => {
    res.render('products/new')

})

app.post('/products', async (req, res) => {
    const newProduct = new Product(req.body)
    await newProduct.save()
    console.log(newProduct)
    res.redirect(`products/${newProduct._id}`)


})

app.get('/products/:id', async (req, res) => {
    const { id } = req.params
    const product = await Product.findById(id)
    res.render('products/details', { product })
})

app.get('/products/:id/edit', async (req, res) => {
    const { id } = req.params
    const product = await Product.findById(id)
    res.render('products/edit', { product })
})

app.put('/products/:id', async (req, res) => {
    const { id } = req.params
    const product = await Product.findByIdAndUpdate(id, req.body, { runValidators: true, new: true })
    res.redirect(`/products/${product._id}`)
})

app.delete('/products/:id', async (req, res) => {
    const { id } = req.params
    const deletedProduct = await Product.findByIdAndDelete(id)
    res.redirect(`/products/`)
})



app.listen(3000)


When paired with Heroku one of the most popular PaaS solutions for developers, you'll be able to build and deploy fully managed cloud applications in no time. The best part? MongoDB Atlas integrates easily with Heroku applications. All you need to do is set your Atlas cluster's connection string to a Heroku config variable . That's really all there is to it!

follow the steps here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM