【问题标题】:How to host a nodeJS ReactJS app on Gandi (or other hosting company)?如何在 Gandi(或其他托管公司)上托管 nodeJS ReactJS 应用程序?
【发布时间】:2018-12-04 17:27:42
【问题描述】:

我第一次不使用 Heroku,但我尝试使用 Gandi 托管我的网站(节点和 express 用于后端,react 用于前端)。 不幸的是,到目前为止,我遇到了 503 错误。 以下是我在 Heroku 上的做法:

Server.js 文件

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 文件:

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

它在 heroku 上运行良好,但我不知道要改变什么才能让它在上面运行
甘地。有人有一些建议或者可能是一个很好的 tuto 知道从哪里开始会很好。 感谢您的帮助。

【问题讨论】:

    标签: node.js reactjs hosting


    【解决方案1】:

    即使我也有同样的问题。 带有 ReactJS 的 Nodejs 是内存密集型应用程序,即使它是一个非常简单的应用程序。

    我认为您正在使用 gandi nodejs 简单托管实例 (https://www.gandi.net/en/simple-hosting) 来运行您的应用程序。 Gandi nodejs 简单托管实例为每个进程提供 128MB 的 RAM。所以你的应用程序会耗尽内存并给你这个 503 错误。

    要解决这个问题,您需要使用至少 1GB RAM 的 gandi 云服务器。购买 gandi cloud 访问这里:https://www.gandi.net/en/cloud

    或者,

    您可以使用提供更多 RAM 的 Heroku、namecheap 等。 Gandi 简单主机无法运作,因为您会耗尽内存。

    【讨论】:

    • 谢谢 Arvindsinc2。到目前为止,我仍在使用 heroku。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 2012-02-06
    相关资源
    最近更新 更多