Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku 503 Service Unavailable

I am currently building a simple CRUD app using ExpressJS, and host it on Heroku using free account.

The problem I ran into is:

  • GET API for getting all items works on localhost, but show status 503 when hosting on Heroku;
  • POST API for updating one item works on localhost, but same issue as GET API above;
  • All 503 errors are after 30s of loading, this should be a setting from Heroku.

I do have other API end points that work on both local and Heroku server:

  • GET API for getting one item using ID

My guessing:

  • The issue should not be a code issue
  • There is some issue when the code is deployed and Heroku cannot process this request

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.

My Mongoose Schema

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);

2 end points that do not work

// Returns all thoughts
router.get("/", async (req, res) => {
  try {
    const thought = await Thought.find();
    res.json(thought);
  } catch (err) {
    res.json({ message: err });
  }
});

// Submit a post
router.post("/", async (req, res) => {
  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 });
  }
});

The end point that works

// Specific thought
router.get("/:thoughtId", async (req, res) => {
  try {
    const thought = await Thought.findById(req.params.thoughtId);
    res.json(thought);
  } catch (err) {
    res.json({ message: err });
  }
});

My package.json for this express app

{
  "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\" && 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"
  }
}

EDIT: My index.js

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 },
  () => {
    console.log("Connected to DB!");
  }
);

app.use(express.json());
app.use((req, res, next) => {
  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) => {
  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) => {
//   res.sendFile(path.join(__dirname, "client/build/index.html"));
// });

const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
  console.log("Listening on port " + PORT + "...");
});
like image 231
Payne Tang Avatar asked Feb 11 '20 00:02

Payne Tang


People also ask

Is Heroku down now?

Current Heroku status is up.

Why am I getting application error on Heroku?

"Application Error" or similar is always caused by your own application code. Routing errors will normally only surface themselves within the logs of your application. In most cases, you will be able to see the cause of the error there. To learn more about logging, please see our Logging article on DevCenter.

Is Heroku free forever?

After offering them for over a decade, Heroku today announced that it will eliminate all of its free services — pushing users to paid plans. Starting November 28, the Salesforce-owned cloud p...


1 Answers

The root cause after checking is due to the access restriction from Heroku to Mongo atlas.

After adding 0.0.0.0/0 for IP whitelist, I am able to get data from MongoDB.

Reference: https://docs.atlas.mongodb.com/security-whitelist/#add-whitelist-entries

like image 151
Payne Tang Avatar answered Oct 18 '22 07:10

Payne Tang