1

I created a RESTful API with Express and MongoDB Atlas and I'm trying to deploy it on Heroku but for some reason it's not working (working on my localhost).

I would like to get some resources or advice to deploy a small RESTful API to then be able to call it from my front-end which is not yet built.

Here is my server.js:

const express = require("express");
const app = express();
const appJs = require("./app");
const port = process.env.PORT || 3000



appJs.listen(port, function() {
  console.log("listening at " + port);
});

and my App.js:


const express = require("express");
const app = express();
const morgan = require("morgan");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");

const jobRoutes = require("../api/routes/jobs");
const userRoutes = require("../api/routes/user");

mongoose.connect(
  `mongodb+srv://myname:${
    process.env.MONGO_ATLAS_PW
  }@cluster0-z4gre.mongodb.net/test?retryWrites=true`,
  {
    useNewUrlParser: true
  }
);

app.use("/uploads", express.static("uploads"));
app.use(morgan("dev"));

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept, Authorization"
  );
  if (req.method === "OPTIONS") {
    res.header("Access-Control-Allow-Methods", "PUT, POST, PATCH, DELETE, GET");
    return res.status(200).json({});
  }
  next();
});

I did all the steps to deploy on Heroku but I got an error message when I go to the domain name.

My restful API is in JSON format and work perfectly in development mode

Here is the error I get when going to the Heroku URL:

Application error

An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command

heroku logs --tail

my logs that fail:

2019-05-04T22:13:54.658480+00:00 heroku[web.1]: Process exited with status 1
2019-05-04T22:13:54.582531+00:00 app[web.1]:
2019-05-04T22:13:54.582551+00:00 app[web.1]: > cryptojobs@1.0.0 start /app
2019-05-04T22:13:54.582553+00:00 app[web.1]: > nodemon ./server/server.js --exec
2019-05-04T22:13:54.582554+00:00 app[web.1]:
2019-05-04T22:13:54.593393+00:00 app[web.1]: sh: 1: nodemon: not found
2019-05-04T22:13:54.598435+00:00 app[web.1]: npm ERR! file sh
2019-05-04T22:13:54.598644+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-05-04T22:13:54.598901+00:00 app[web.1]: npm ERR! errno ENOENT
2019-05-04T22:13:54.599158+00:00 app[web.1]: npm ERR! syscall spawn

nodemon is installed as a devDependencies.

Community
  • 1
  • 1
Yan Nis
  • 11
  • 1

1 Answers1

1

By default, Heroku removes devDependencies before deploying your application. If you need something in production, it probably belongs in your dependencies, not devDependencies.

But you probably shouldn't be using nodemon in production anyway. It's a development tool.

Instead, consider changing your start script to use node instead of nodemon, e.g.

"start": "node ./server/server.js"

You can add a dev script to use nodemon while you're editing that file if you want. That way you can use npm run dev or yarn dev on your local machine to keep your old behaviour.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257