[英]How to run Express app with React in production mode
I have this web app that has this file structure. 我有这个具有此文件结构的Web应用程序。
how can I run my web app that has the Production Build of React? 如何运行具有React的Production Build的Web应用程序? This code in my index.js doesn't work.
我的index.js中的此代码不起作用。
app.use(express.static("client/build"));
app.get("/", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
should I execute the command serve -s build
? 我应该执行命令
serve -s build
吗? in the root directory? 在根目录?
Replace your index.js
with this. 以此替换您的
index.js
。
const express = require('express');
const path = require('path');
const port = process.env.PORT || 3500;
const app = express();
app.use(express.static(__dirname + '/build'));
app.get('*', function (request, response){
response.sendFile(path.resolve(__dirname, 'build', 'index.html'))
});
app.listen(port);
and then command node index.js
然后命令
node index.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.