部署MERN堆栈应用程序的问题

时间:2020-02-13 04:45:38

标签: node.js reactjs heroku mern react-fullstack

我无法部署我的全栈应用程序。

就文件夹结构而言,我的存储库看起来完全像这样:

以下是我的 package.json:https://github.com/bradtraversy/mern_shopping_list

{
  "name": "clear",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start:prod": "NODE_ENV=production nodemon server.js",
    "debug": "ndb server.js",
    "server": "nodemon server.js",
    "client": "cd client && npm start",    
    "build": "cd client && npm run build",    
    "dev": "concurrently --kill-others-on-fail \"npm run server\" \"npm run client\"",
    "start": "node server.js",
    "heroku-postbuild": "cd client && npm install && npm install --only=dev --no-shrinkwrap && npm run build"    
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.19.0",
    "compression": "^1.7.4",
    "concurrently": "^5.1.0",
    "cookie-parser": "^1.4.4",
    "cors": "^2.8.5",
    "dotenv": "^8.1.0",
    "express": "^4.17.1",
    "express-mongo-sanitize": "^1.3.2",
    "express-rate-limit": "^5.0.0",
    "helmet": "^3.21.1",
    "hpp": "^0.2.2",
    "html-to-text": "^5.1.1",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.7.1",
    "morgan": "^1.9.1",
    "multer": "^1.4.2",
    "ndb": "^1.1.5",
    "nodemailer": "^6.3.0",
    "path": "^0.12.7",
    "pug": "^2.0.4",
    "slugify": "^1.3.5",
    "stripe": "^7.9.1",
    "validator": "^11.1.0",
    "xss-clean": "^0.1.1"
  },
  "engines": {
    "node": "^10"
  }
}

1 个答案:

答案 0 :(得分:1)

你可以试试这个吗?

这是server.js文件的内容:-

const express = require("express");
const path = require("path");
const app = express();
require("dotenv").config();
app.use(express.static(path.join(__dirname, "build")));

app.get("/", function(req, res) {
  res.sendFile(path.join(__dirname, "build", "index.html"));
});

app.get("/*", function(req, res) {
  res.sendFile(path.join(__dirname, "build", "index.html"));
});

app.listen(process.env.PORT || 3000);

那是我的脚本部分:-

  "scripts": {
    "start:production": "npm install && npm run build && npm run start:prod",
    "start:prod": "cross-env NODE_ENV=production node server",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

生产中的启动命令:-

npm run start:production