I
I
IlyaMalyukov2022-03-31 00:37:56
PostgreSQL
IlyaMalyukov, 2022-03-31 00:37:56

Trouble deploying node js API to heroku. How to set up correctly?

I'm new to express js
. I'm trying to deploy my project to Heroku
. But due to incorrect database settings, requests get 503

index.js errors.

require('dotenv').config()
const express = require('express')
const sequelize = require('./db')
const models = require('./models/models')
const cors = require('cors')
const fileUpload = require('express-fileupload')
const router = require('./routes/index')
const errorHandler = require('./middleware/ErrorHandlingMiddleware')
const path = require('path')

const PORT = process.env.PORT || 80

const app = express()

app.get('/', (req, res) => {
  res.end('<h1>Home</h1>')
})

app.use(cors())
app.use(express.json())
app.use(express.static(path.resolve(__dirname, 'static')))
app.use(fileUpload({}))
app.use('/api', router)


// Обработка ошибок, последний middleware
app.use(errorHandler)

const start = async () => {
  try {
    await sequelize.authenticate()
    await sequelize.sync()
    app.listen(PORT, () => console.log(`Server started on port ${PORT}`))
  } catch(e) {
    console.log(e)
  }
}
start()


db.js
const {Sequelize} = require('sequelize')

module.exports = new Sequelize(
  process.env.DB_NAME,
  process.env.DB_USER,
  process.env.DB_PASSWORD,
  {
    dialect: 'postgres',
    host: process.env.DB_HOST,
    port: process.env.DB_PORT
  }
)


In the .env file, the variable DB_HOST = localhost
I think the problem is somewhere here

. more than a year ago I tried to deploy the application on ROR and in the config on the Heroku website I had to specify the database URL and the secret key
Where to get all this, I can’t remember anymore and find at least some information about it

somewhere

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question