简体   繁体   English

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

[英]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.

相关问题 如何将 React 置于生产模式? - How to put React in production mode? create-react-app npm在生产模式下运行。 也许不可能? - create-react-app npm run start in production mode. Maybe not possible? 电子在生产模式下运行 - Electron run in production mode Angular / JS Express应用程序在生产模式下处于无限路由循环中,在开发模式下运行良好 - Angular/JS Express app in infinite routing loop in production mode, works fine in development mode 在生产模式下运行 expo android 应用程序时出错 - React-native - Error running expo android app in production mode - React-native React.js 应用程序在开发模式下运行良好,但在构建(生产模式)后出现错误 - React.js app works fine in development mode but has errors after building (production mode) 如何在生产期间在 electron 中路由反应应用程序 - How to route in electron react app during production 如何在使用Gulp时将React设置为生产模式 - How to set React to production mode when using Gulp 如何在运行时检测 React 中的开发模式与生产模式 - How to detect development vs production mode in React at runtime React 应用程序无法通过其请求在生产中访问我的快速应用程序,但在开发中运行良好 - React app can't reach my express app in production with its requests but works fine in development
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM