Heroku Express Create-React-App Mlab 部署 - 无法获取
Heroku Express Creat-React-App Mlab deployment - cannot GET
正在寻求帮助。我刚刚将我的第一个 react/express/mongo 应用程序部署到 heroku,但它加载时无法获取,而且我似乎看不到任何表明原因的错误。
这是我的文件夹结构:
我的顶级 package.json 文件看起来像
{
"name": "russia_2018",
"version": "1.0.0",
"description": "",
"main": "index.js",
"engines": {
"node": "9.5.0"
},
"scripts": {
"build": "concurrently \"cd client && yarn build\" \"cd server &&
yarn build\"",
"clean": "concurrently \"rimraf node_modules\" \"cd client && rimraf
node_modules build\" \"cd server && rimraf node_modules build\"",
"heroku-postbuild": "yarn build",
"install": "(cd client && yarn) && (cd server && yarn)",
"start": "concurrently \"cd client && PORT=3000 yarn start\" \"cd
server && PORT=3001 yarn start\"",
"start:prod": "cd server && yarn start:prod",
"heroku-postbuild": "cd client && yarn && yarn run build"
}
过程文件:
web: yarn start:prod
服务器package.json
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "babel . --ignore node_modules,build --out-dir build",
"start": "nodemon -r babel-register server.js",
"start:prod": "node server.js",
"seeds": "mongo < db/seeds.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^1.17.5"
},
"dependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"body-parser": "^1.18.3",
"express": "^4.16.3",
"mongod": "^2.0.0",
"mongodb": "^3.1.1"
}
}
和server.js:
const express = require('express');
const app = express();
const path = require('path');
const parser = require('body-parser');
const MongoClient = require('mongodb').MongoClient;
const createRouter = require('./helpers/create_router.js');
const staticFiles = express.static(path.join(__dirname,
'../../client/build'))
app.use(staticFiles)
app.use(parser.json())
MongoClient.connect('mongodb://full-url...
||'mongodb://localhost:27017'', (err, client) => {
if(err){
console.log(err);
}
const db = client.db('russia');
const games = db.collection('games');
const gamesRouter = createRouter(games);
app.use('/api/games', gamesRouter);
app.use('/*', staticFiles)
app.listen(process.env.PORT || 3001, function(){
console.log('listening on port 3001');
})
});
我看不出错误是从哪里来的,因为日志显示该应用程序已正常连接到主路由。唯一看起来可能是错误的行是 Stopping all processes with SIGTERM
,但应用程序在那之后继续连接。
谢谢
似乎 heroku 从未获取您的构建文件夹。您的 procfile 为 herkou 指定此命令以调用部署 web: yarn start:prod
。查看您的顶层 package.json 似乎 start:prod
仅执行此操作 "start:prod": "cd server && yarn start:prod"
,其中不包含有关构建 React 应用程序的任何说明。
您可能需要更改您的 procfile 以调用一个命令,该命令将为您构建客户端文件夹,然后在完成后启动服务器。
你也可以在本地构建文件夹,然后从客户端.gitignore 中删除它,这样 heroku 就可以启动服务器并且已经有可用的构建文件夹。
您可能已经看过 this 文章,但如果没有,这也是关于该主题的一个很好的读物。
正在寻求帮助。我刚刚将我的第一个 react/express/mongo 应用程序部署到 heroku,但它加载时无法获取,而且我似乎看不到任何表明原因的错误。
这是我的文件夹结构:
我的顶级 package.json 文件看起来像
{
"name": "russia_2018",
"version": "1.0.0",
"description": "",
"main": "index.js",
"engines": {
"node": "9.5.0"
},
"scripts": {
"build": "concurrently \"cd client && yarn build\" \"cd server &&
yarn build\"",
"clean": "concurrently \"rimraf node_modules\" \"cd client && rimraf
node_modules build\" \"cd server && rimraf node_modules build\"",
"heroku-postbuild": "yarn build",
"install": "(cd client && yarn) && (cd server && yarn)",
"start": "concurrently \"cd client && PORT=3000 yarn start\" \"cd
server && PORT=3001 yarn start\"",
"start:prod": "cd server && yarn start:prod",
"heroku-postbuild": "cd client && yarn && yarn run build"
}
过程文件:
web: yarn start:prod
服务器package.json
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "babel . --ignore node_modules,build --out-dir build",
"start": "nodemon -r babel-register server.js",
"start:prod": "node server.js",
"seeds": "mongo < db/seeds.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"nodemon": "^1.17.5"
},
"dependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"body-parser": "^1.18.3",
"express": "^4.16.3",
"mongod": "^2.0.0",
"mongodb": "^3.1.1"
}
}
和server.js:
const express = require('express');
const app = express();
const path = require('path');
const parser = require('body-parser');
const MongoClient = require('mongodb').MongoClient;
const createRouter = require('./helpers/create_router.js');
const staticFiles = express.static(path.join(__dirname,
'../../client/build'))
app.use(staticFiles)
app.use(parser.json())
MongoClient.connect('mongodb://full-url...
||'mongodb://localhost:27017'', (err, client) => {
if(err){
console.log(err);
}
const db = client.db('russia');
const games = db.collection('games');
const gamesRouter = createRouter(games);
app.use('/api/games', gamesRouter);
app.use('/*', staticFiles)
app.listen(process.env.PORT || 3001, function(){
console.log('listening on port 3001');
})
});
我看不出错误是从哪里来的,因为日志显示该应用程序已正常连接到主路由。唯一看起来可能是错误的行是 Stopping all processes with SIGTERM
,但应用程序在那之后继续连接。
谢谢
似乎 heroku 从未获取您的构建文件夹。您的 procfile 为 herkou 指定此命令以调用部署 web: yarn start:prod
。查看您的顶层 package.json 似乎 start:prod
仅执行此操作 "start:prod": "cd server && yarn start:prod"
,其中不包含有关构建 React 应用程序的任何说明。
您可能需要更改您的 procfile 以调用一个命令,该命令将为您构建客户端文件夹,然后在完成后启动服务器。
你也可以在本地构建文件夹,然后从客户端.gitignore 中删除它,这样 heroku 就可以启动服务器并且已经有可用的构建文件夹。
您可能已经看过 this 文章,但如果没有,这也是关于该主题的一个很好的读物。