Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

javascript - 如何从我们的服务器提供React应用?(How can I serve a React app from our server?)

This is more of a high-level question.(这更多是一个高层问题。)

I want to make an "Intercom like" app -- that is, the app is loaded through a unique script tag URL from our server, and is then run on the client page.(我想制作一个“像对讲机一样”的应用程序-也就是说,该应用程序是通过服务器上的唯一脚本标记URL加载的,然后在客户端页面上运行。) My question is, on a high-level, how would I go about doing this?(我的问题是,从高层次上讲,我将如何去做?) For example, if the client page has an organization id (like Intercom does), then on the server are we just generating a JS file with that id and serving that?(例如,如果客户端页面具有组织ID(如Intercom一样),那么在服务器上,我们是否只是生成具有该ID的JS文件并将其提供服务?) If so, how do we auto-generate this file?(如果是这样,我们如何自动生成此文件?) I'm a bit confused about how it all fits together and any insight would be greatly appreciated!(我对所有这些如何组合感到困惑,任何见解将不胜感激!)   ask by Colin Ricardo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to create a node js server, build your react app & serve it.(您需要创建一个节点js服务器,构建您的react应用并为其提供服务。)

const express = require('express'); const path = require('path'); const app = express(); // Serve the static files from the React app app.use(express.static(path.join(__dirname, 'client/build'))); app.get('*', (req,res) =>{ res.sendFile(path.join(__dirname+'/client/build/index.html')); });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...