簡體   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