简体   繁体   中英

React.js with Node.js Rest API

Should I include my Reactjs app in the same application as my REST API?

I am new to Reactjs. I have built a few standalone Node REST APIs. I am building a React app, an Android app and an iOS app (so the REST API needs to service all three applications). I was wondering what convention dictates when building the Node API.

There are 2 options as I see it...

1) I could build one large application that encompasses the REST API and React App. This app would service all the API calls from the mobile apps and react/web app. It would also serve all the views aka HTML, CSS, etc..

OR

2) I could build two smaller apps. One would serve the React app with the view templates with a separate (lighter) Node server. The second app would not hold any views. It just work as a JSON API to serve all three apps.

I would recommend going with an isomorphic application. Check out this boiler plate: react-redux-universal-hot-example .

In your project, you can have two ports running: one for rendering your react views (I prefer server side rendering to provide faster web rendering) and the other for apis and web sockets.

Your mobile apps can also use the api/ws server for RESTful services.

I recommend building the single application for both the frontend and backend. Once scaling becomes an issue, then separating the two makes more sense because you'll want the frontend content served from a CDN.

But for something small, separating frontend and backend would require you to have to run two separate servers and deal with CORS: https://en.wikipedia.org/wiki/Cross-origin_resource_sharing

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