【问题标题】:How to run Express app with React in production mode如何在生产模式下使用 React 运行 Express 应用
【发布时间】:2018-09-27 01:55:51
【问题描述】:

我有这个文件结构的网络应用程序。

如何运行具有 React 生产版本的 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吗?在根目录下?

【问题讨论】:

    标签: javascript node.js reactjs express


    【解决方案1】:

    用这个替换你的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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-10
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2012-04-19
      • 1970-01-01
      相关资源
      最近更新 更多