简体   繁体   English

如何通过一台 express 服务器部署不同的 react web 应用程序?

[英]How to deploy different react web apps through one express server?

I'm wondering if there is an easy way to deploy an express server that serves different apps on different routes.我想知道是否有一种简单的方法可以部署在不同路线上为不同应用程序提供服务的快速服务器。 (Not a front end and and api.) So, for instance let's say I wanted to run a "Hello World." (不是前端和 api。)例如,假设我想运行“Hello World”。 app through a finite number of routes (like a home/landing page) and then deploy a "My app" app through a certain URL route with each react app using react router.应用程序通过有限数量的路由(如主页/登录页面),然后通过某个 URL 路由部署“我的应用程序”应用程序,每个反应应用程序使用反应路由器。 Is this possible?这可能吗? If so, how can it be done?如果是这样,怎么办?

U should provide homepage in package.json for each react-app, for example你应该在package.json中为每个 react-app 提供homepage ,例如


__
 |_ app1/
 |_ app2/
 |_ server/

for app1 package.json homepage: "/app1"对于 app1 package.json homepage: "/app1"

for app2 package.json homepage: "/app2"对于 app2 package.json homepage: "/app2"

Express server code:快递服务器代码:

app.use("/app1", express.static(path.resolve(__dirname, "../app1/build")))
app.use("/app2", express.static(path.resolve(__dirname, "../app2/build")))

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

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

I hope I was able to help我希望我能提供帮助

Edit:编辑:

Also you can provide basename prop for BrowserRouter or HashRouter您还可以为BrowserRouterHashRouter提供basename属性

For app1 basename="app1" and for app2 basename="app2"对于 app1 basename="app1"和 app2 basename="app2"

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

相关问题 如何将响应应用程序部署到localhost中的apache服务器 - How to deploy react apps into apache server in localhost 如何将通过 Node/Express 进行服务器调用的 React 应用程序部署到 IIS? - How to deploy React application that makes server calls through Node/Express to IIS? 如何在 Apache Web 服务器上部署 React 应用程序 - How to deploy a React App on Apache web server ExpressJS-在同一Express服务器上为具有不同基本目录的多个Web应用程序提供服务 - ExpressJS - Serving Multiple Web Apps with Different Base Directories on the Same Express Server 如何使用Webpack部署Express + React应用 - How to use webpack to deploy an Express + React app 如何将 React + NodeJS Express 应用程序部署到 AWS? - How to deploy a React + NodeJS Express application to AWS? 如何将 express.js 服务器部署到 Netlify - How to deploy express.js server to Netlify 服务器上带有react.js的交互式Web应用程序 - Interactive web apps with react.js on the server 在 MPA 中反应上下文(不同应用程序的一个上下文) - React context in MPA (one context for different apps) 我如何将一组对象传递到我的 express/sql 服务器(反应) - How could I pass an array of objects through to my express/sql server (react)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM