简体   繁体   English

ReactJS和NodeJS是同一台服务器?

[英]ReactJS and NodeJS same server?

I build a project with nodejs and react. 我用nodejs构建一个项目并做出反应。 I don't know if I must split in 2 servers, one for the react app and one for API in nodejs and the react app ask request at nodejs server. 我不知道我是否必须拆分为2台服务器,其中一台用于react app,一台用于nodejs中的API,react app在nodejs服务器上提出请求。

Or I must group both in only one nodejs process ? 还是我只能将它们归入一个nodejs进程中? What's the difference ? 有什么不同 ? There are a better choice ? 有更好的选择吗?

It's up to you React when builded is just static files, don't get confused by the development server. 当构建的只是静态文件时,由您决定React不会被开发服务器所迷惑。 I would recommend you for the beginning to put them in one node process. 我建议您一开始将它们放在一个节点进程中。 Just declare the folder of the static files like this: 只需这样声明静态文件的文件夹:

app.use('/app', express.static(path.join(__dirname + '/dist/app')));

Also if you are using React Router you should add this as your last router 另外,如果您使用的是React Router,则应将其添加为最后一个路由器

app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/src/index.html'));
});

You can check my template repo with webpack here 您可以在此处使用webpack检查我的模板仓库

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM