将React和Nodejs部署到同一个Heroku Dyno

时间:2018-03-20 20:27:47

标签: javascript node.js reactjs heroku

我尝试在this指南之后将React应用程序和Nodejs后端部署到同一个Heroku应用程序。一切似乎工作正常,应用程序构建成功,但它只服务于项目的后端(api)部分,而不是前端。

这是我的package.json,它位于客户端和服务器文件夹之外。

{
  "name": "web-back",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "client": "cd react-ui && npm start",
    "dev-server": "cd server && nodemon index.js",
    "dev": "concurrently --kill-others-on-fail \"npm run dev-server\" \"npm run client\"",
    "start": "node server",
    "heroku-postbuild": "cd react-ui/ && npm install && npm install --only=dev --no-shrinkwrap && npm run build"
  },
  "cacheDirectories": [
    "node_modules",
    "react-ui/node_modules"
  ],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.3",
    "mongoose": "^5.0.11"
  }
}

这是server / index.js

const express       = require('express'),
      mongo         = require('mongoose');

const app = express();
const port = process.env.PORT || 5000;
const mongoURL = process.env.DBURL || "mongodb://localhost/test"; 

mongo.connect(mongoURL); 

const CommandSchema = mongo.Schema({
    Command: String, 
    Text: String
}, { collection: "Commands" });

const Command = mongo.model("Command", CommandSchema); 

app.get('/api', (req, res) => {
    Command.find({}, (err, result) => {
        if(err) res.send(err);
        else res.send(result); 
    });
});

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

这是我的文件夹结构。

enter image description here

设置时我是否遗漏了什么?

0 个答案:

没有答案