'How to fix "503 Service Unavailable" error on Postman for server hosted on heroku

If I host this server locally, it works perfectly on Postman (and stores the data on MongoDB), however, once hosted on Heroku, it gives me a 503 error. Does anyone have any ideas about what to do?

Local Host Working #1

Local Host Working #2

Local Host Working #3

503 Error Occurring

I've heard from other's that it might be a CORS error. Not sure about the relation between CORS and Postman however. Here's my package.json, just in case:

{
  "name": "login_server",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.0.1",
    "cors": "^2.8.5",
    "dotenv": "^16.0.0",
    "express": "^4.18.1",
    "mongoose": "^6.3.2",
    "nodemon": "^2.0.16"
  }
}

And, here's my server.js:


    //mongodb
    require('./config/db');
    
    const app = require('express')();
    const port = process.env.PORT || 3000;
    
    // cors accesss to enable application on different hosts to send request
    const cors = require("cors");
    app.use(cors());
    
    const UserRouter = require('./api/User');
    
    // For accepting post form data
    const bodyParser = require('express').json;
    app.use(bodyParser());
    
    app.use('/user', UserRouter);
    
    app.listen(port, () => {
        console.log(`Server running on port ${port}`);
    })

Thanks so much in advance. Cheers.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source