我读了很多关于Heroku上的MERN应用程序部署的博客,但是它们都使用单独的package.json用于客户端和服务器!
是否可以使用一个package.json文件?
我的package.json
{
"name": "ecommerce",
"version": "0.1.0",
"private": true,
"author": "Dweep Panchal",
"license": "ISC",
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1",
"braintree-web-drop-in-react": "^1.1.1",
"concurrently": "^5.2.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1",
"react-stripe-checkout": "^2.6.3",
"body-parser": "^1.19.0",
"braintree": "^2.22.0",
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-jwt": "^5.3.3",
"express-validator": "^6.4.0",
"formidable": "^1.2.2",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.15",
"mongoose": "^5.9.7",
"stripe": "^8.46.0",
"uuid": "^7.0.3"
},
"scripts": {
"server": "cd ./backend && node app.js",
"start": "concurrently \"npm run server\" \"react-scripts start\"",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:8000",
"devDependencies": {
"nodemon": "^2.0.3"
},
"engines": {
"node": "10.16.0",
"npm": "6.9.0"
}
}
app.js
require("dotenv").config({ path: "../.env" });
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const app = express();
// Routes
const authRoutes = require("./routes/auth");
const userRoutes = require("./routes/user");
const categoryRoutes = require("./routes/category");
const productRoutes = require("./routes/product");
const orderRoutes = require("./routes/order");
const stripeRoutes = require("./routes/stripepayment");
const braintreeRoutes = require("./routes/braintreepayment");
// DB Connection
mongoose
.connect(
`mongodb+srv://${process.env.DB_NAME}:${process.env.DB_PASS}@${process.env.DB_PROJECT}-xi8tq.mongodb.net/${process.env.DB_PROJECT}?retryWrites=true&w=majority`,
{
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: true,
}
)
.then(() => console.log("DB Connected!"))
.catch(() => console.log("Failed to Connect DB"));
// Middleware
app.use(bodyParser.json());
app.use(cookieParser());
app.use(cors());
// My Routes
app.use("/api", authRoutes);
app.use("/api", userRoutes);
app.use("/api", categoryRoutes);
app.use("/api", productRoutes);
app.use("/api", orderRoutes);
app.use("/api", stripeRoutes);
app.use("/api", braintreeRoutes);
// Server Connection
const port = process.env.BACKEND_PORT || 8000;
app.listen(port, () => console.log(`Server Running at Port ${port}`));
当我在heroku上部署此应用程序时,项目URL显示:
无效的主机标头
答案 0 :(得分:1)
让我快速解释一下如何部署任何Reactjs并表达给heroku工作。目标是使用npm run build生成一个构建文件夹,然后启动服务器以从该构建文件夹提供静态内容。
在heroku文档中,它指出heroku-postbuild在启动脚本之前运行。这是执行npm run build生成生成文件夹,然后使用启动脚本运行服务器代码的理想场所。从那里,服务器应使用express.static并指向生成生成文件夹的位置。
这就是为什么人们喜欢使用服务器package.json的原因,因为它不会干扰react start脚本。现在,我立即看到的问题是您没有正确使用脚本,也没有指向您要生成的构建文件夹。
在package.json中,创建一个启动脚本以启动app.js文件,然后创建一个heroku-postbuild来生成构建文件夹。像下面一样
"scripts": {
"start": "cd ./backend && node app.js",
"heroku-postbuild": "npm run build",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
之后,您的app.js中应该有app.use(express.static(<<location of build folder>>))
。在您的app.js中添加以下代码
if (process.env.NODE_ENV === "production") {
app.use(express.static("../build"));
}
我说../build的原因是因为您的build文件夹在后端文件夹之外。
我还检查了您的整个代码,还有其他小的配置问题。例如,如果要部署到heroku,则应使用process.env.PORT
答案 1 :(得分:0)
是的,如果后端为您的前端服务是可能的。当您的后端和前端在两个单独的节点程序上运行时,两个package.json
很有用。
invalid host headers
可能是由于服务器处理标头的方式所致,但是由于您尚未发布minimal reproducible example,因此到目前为止,我们无法为您提供进一步的帮助。