How to host a nodeJS ReactJS app on Gandi (or other hosting company)?

Viewed 766

For the first time, I don't use Heroku but I try to host my website (node and express for the back and react for the front) with Gandi. Unfortunately, so far I have a 503 error. Here is how I use to do on Heroku:

Server.js file

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

const app = express();

// Body parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

const profiles = require("./routes/profiles");
const categories = require("./routes/categories");
const skills = require("./routes/skills");
const message = require("./routes/message");

// DB config
const db = require("./config/keys.js").mongoURI;

// Connect to mongoDB
mongoose
  .connect(
    db,
    { useNewUrlParser: true }
  )
  .then(() => console.log("MongoDB connected"))
  .catch(err => console.log(err));

// Use Routes
app.use("/profiles", profiles);
app.use("/categories", categories);
app.use("/skills", skills);
app.use("/message", message);

// Server static assets if in production
if (process.env.NODE_ENV === "production") {
  // Set static folder
  app.use(express.static("client/build"));
  app.get("*", (req, res) => {
    res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
  });
}

const port = process.env.PORT || 5000;

app.listen(port, () => console.log(`Server running on port ${port}`));

Package.json file:

{
  "name": "adopt-a-dev",
  "version": "1.0.0",
  "description": "Find the best dev for your company",
  "main": "server.js",
  "scripts": {
    "client-install": "npm install --prefix client",
    "start": "node server.js",
    "server": "nodemon server.js",
    "client": "npm start --prefix client",
    "dev": "concurrently \"npm run server\" \"npm run client\"",
    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
  },
  "author": "Guillaume Cogito",
  "license": "MIT",
  "dependencies": {
    "body-parser": "^1.18.3",
    "concurrently": "^3.6.1",
    "express": "^4.16.3",
    "mongoose": "^5.2.7",
    "nodemailer": "^4.6.8",
    "validator": "^10.6.0"
  },
  "devDependencies": {
    "nodemon": "^1.18.3"
  }
}

It works well on heroku but I don't know what to change to make it works on
Gandi. It someone have some advice or maybe a good tuto to know where to start it would be nice. Thank you for your help.

1 Answers

Even I had the same issue. Nodejs with ReactJS is memory intensive application even if it's a very simple application.

I think you are using gandi nodejs simple hosting instance (https://www.gandi.net/en/simple-hosting) to run your app. Gandi nodejs simple hosting instance provides 128MB of RAM per process. So your application will run out of memory and give you this 503 error.

To solve this you need to use gandi cloud server with minimum 1GB RAM. To buy gandi cloud visit here: https://www.gandi.net/en/cloud

Alternatively,

You can use Heroku, namecheap etc, which provides more RAM. Gandi simple hosting wont work because you will run out of memory.

Related