简体   繁体   中英

ReactJS and NodeJS same server?

I build a project with nodejs and react. 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.

Or I must group both in only one nodejs process ? 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. 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

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

You can check my template repo with webpack here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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