繁体   English   中英

如何在生产模式下使用React运行Express应用

[英]How to run Express app with React in production mode

我有这个具有此文件结构的Web应用程序。

在这里

如何运行具有React的Production Build的Web应用程序? 我的index.js中的此代码不起作用。

app.use(express.static("client/build"));

app.get("/", (req, res) => {
    res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});

我应该执行命令serve -s build吗? 在根目录?

以此替换您的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);

然后命令node index.js

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM