title: Heroku 503 Service Unavailable
body: <p>I am currently building a simple CRUD app using ExpressJS, and host it on Heroku using free account.</p>
  
  <p>The problem I ran into is:</p>
  
  <ul>
  <li>GET API for getting all items works on localhost, but show status 503 when hosting on Heroku;</li>
  <li>POST API for updating one item works on localhost, but same issue as GET API above;</li>
  <li>All 503 errors are after 30s of loading, this should be a setting from Heroku.</li>
  </ul>
  
  <p>I do have other API end points that work on both local and Heroku server:</p>
  
  <ul>
  <li>GET API for getting one item using ID</li>
  </ul>
  
  <p>My guessing:</p>
  
  <ul>
  <li>The issue should not be a code issue</li>
  <li>There is some issue when the code is deployed and Heroku cannot process this request</li>
  </ul>
  
  <p><strong>I tried to find some articles on the web but this seems hard to diagnose, anyone who has experience please let me know how I can solve this issue. Appreciate your comments.</strong></p>
  
  <p><strong>My Mongoose Schema</strong></p>
  
  <pre><code>const mongoose = require("mongoose");
  
  const ThoughtSchema = mongoose.Schema({
    title: {
      type: String,
      required: true
    },
    content: {
      type: String,
      required: true
    },
    date: {
      type: Date,
      default: Date.now
    }
  });
  
  module.exports = mongoose.model("Thought", ThoughtSchema);
  </code></pre>
  
  <p><strong>2 end points that do not work</strong></p>
  
  <pre><code>// Returns all thoughts
  router.get("/", async (req, res) =&gt; {
    try {
      const thought = await Thought.find();
      res.json(thought);
    } catch (err) {
      res.json({ message: err });
    }
  });
  
  // Submit a post
  router.post("/", async (req, res) =&gt; {
    const thought = new Thought({
      title: req.body.title,
      content: req.body.content
    });
  
    try {
      const savedThought = await thought.save();
      res.json(savedThought);
    } catch (err) {
      res.json({ message: err });
    }
  });
  </code></pre>
  
  <p><strong>The end point that works</strong></p>
  
  <pre><code>// Specific thought
  router.get("/:thoughtId", async (req, res) =&gt; {
    try {
      const thought = await Thought.findById(req.params.thoughtId);
      res.json(thought);
    } catch (err) {
      res.json({ message: err });
    }
  });
  </code></pre>
  
  <p><strong>My package.json for this express app</strong></p>
  
  <pre><code>{
    "name": "my-thoughts-app",
    "version": "0.1.0",
    "description": "An app to records user's thoughts",
    "main": "index.js",
    "scripts": {
      "start": "node index.js",
      "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
    },
    "repository": {
      "type": "git",
      "url": "git+https://github.com/PayneTang/my-thoughts-app.git"
    },
    "author": "Payne Tang",
    "license": "ISC",
    "bugs": {
      "url": "https://github.com/PayneTang/my-thoughts-app/issues"
    },
    "homepage": "https://github.com/PayneTang/my-thoughts-app#readme",
    "dependencies": {
      "dotenv": "^8.2.0",
      "express": "^4.17.1",
      "mongoose": "^5.8.11"
    },
    "devDependencies": {
      "typescript": "^3.7.5"
    }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong>
  <strong>My index.js</strong></p>
  
  <pre><code>const express = require("express");
  const path = require("path");
  const app = express();
  const mongoose = require("mongoose");
  const thoughtRoute = require("./routes/thought");
  require("dotenv").config();
  
  console.log(process.env);
  
  // Mongoose settings
  mongoose.connect(
    process.env.DB_CONNECTION,
    { useNewUrlParser: true, useUnifiedTopology: true },
    () =&gt; {
      console.log("Connected to DB!");
    }
  );
  
  app.use(express.json());
  app.use((req, res, next) =&gt; {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "*");
    res.header(
      "Access-Control-Allow-Headers",
      "Origin, X-Requested-With, Content-Type, Accept"
    );
    next();
  });
  
  app.use("/api/thought", thoughtRoute);
  app.get("/api/test", (req, res) =&gt; {
    res.send("hi");
  });
  
  // Serve client side
  app.use(express.static(path.join(__dirname, "client/build")));
  app.use(express.static(path.join(__dirname, "client/public")));
  // app.get("*", (req, res) =&gt; {
  //   res.sendFile(path.join(__dirname, "client/build/index.html"));
  // });
  
  const PORT = process.env.PORT || 5000;
  app.listen(PORT, () =&gt; {
    console.log("Listening on port " + PORT + "...");
  });
  </code></pre>