-
-
Notifications
You must be signed in to change notification settings - Fork 27k
How-to recipes: Using a Node server *and* deploying on heroku #639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hello, @vrk! Also there is workaround, you can serve your react app |
Oh awesome! Thanks for the fast reply. I'll give that express.static thing a try in the next day or two and will report back! |
Well that went a lot faster that I thought. Thank you, got it working now! That's just what I needed to know. For posterity in case someone else comes across this issue, what I did was the following:
I'm trying to decide if there's a tutorial I can contribute to the User Guide section of this project , maybe simply an addendum to the Integrating with a Node Backend section that explains the changes needed if someone wants to deploy that to Heroku. I'll keep this open for now for this reason. |
@vrk Never mind, I got it deployed on Heroku using a server that serves the static build. For anyone else who wants to deploy the original 'create-react-app with a server example' (https://github.com/fullstackreact/food-lookup-demo) to Heroku:
Now when you deploy to Heroku, node will start your server and the client dependencies including |
Maybe worth including this into that guide? |
@CaliAlec Awesome, glad you got it working! @gaearon / @acco One thing that wasn't obvious to me until later: the fact that you don't have to/shouldn't run a server for your React apps made through create-react-app, and that the server launched through create-react-app is strictly for debugging. I think it'd be helpful to mention that more clearly in the following places:
Feel free to do what you want with those suggestions, including ignore them :) I'm also happy to help with a PR for README.md. |
@gaearon Good idea, time has come to add a Heroku/deployments section to the post. @vrk Thanks for starting this discussion and for all the info! There are two main approaches developers will take here. The first is along the lines that you took — have your node server serve your static assets (the result of However, the production deployment we had in mind was a little different. You can use Some hurdles that come to mind with that approach: Handling CORS if you don't have a domain name (using Heroku free tier) and making the dual target deployment strategy palatable. In any case, I'll drop a note here when we've expanded our post to include deployments. |
@acco I think a lot of quick (small) projects probably don't bother with a static asset server split from the app server. Especially if you're going to deploy to some fast PaaS setup. Furthermore, handling multiple server processes just makes everything much more complicated if you're going to run things through an end-to-end test or something. So, I sorta think supporting a single app server that handles client + server is a better developer experience even if in a more disciplined prod deployment, most people would want to get a static app server up and going. With that in mind, what about making react-scripts expose a middleware that's analogous to The method suggested here https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/ (which is linked from the official docs) where you create a nested node.js feels more complicated than necessary. I bet it also would break a lot of auto-detect code in common infrastructure like Travis CI and its default recipes. For example, Travis CI's caching documentation for node.js suggests manually specifying the If instead we exported a middleware, similar to how I imagine a pseudo code looking something like:
Followed by a modification to package.json so that start no longer directly calls This way a fullstack eng could hook into the react-scripts environment programmatically without breaking hermeticity. Thoughts? |
I don’t think we’re at the point where we’re ready to expose any APIs from |
@gaearon understood about the dangers with that, but: (a) this is a fairly narrow API since the semantics are just "mount the equiv of react start on this path" I feel like it might be worth considering bumping the priority a bit. |
Thanks for all the info and replies! @acco: Makes sense. Thanks, and looking forward to seeing what you come up with!
My drive-by opinion: I agree with @awong-dev. Recommending a deployment solution that would involve 2 Heroku instances (or S3/Heroku combo, or some other pure separation) seems to go slightly against the "simple setup!" spirit of create-react-app. It seems it'd be nice if you could support a way for create-react-app to work harmoniously on a simple monolithic server. (But of course, these things are super easy for me to say as a drive-by observer of this project 😜 ) |
Hey @awong-dev @vrk — Thanks for the thoughts! Totally valid points. I do think it would be great if a developer could avoid working with both S3 and Heroku to deploy a create-react-app app. S3/credentials/s3cmd are all a pain to setup for the first time. There is a Heroku integration that alleviates most of this (Bucketeer) but it's not free. For a simple, free, one-dyno setup, having your "API" server also serve static assets is the only option (as @vrk highlighted). So it just means there will be some divergence between the setup of your app in development vs production. (You could close this divergence by having your Node+Express server proxy requests to Webpack in development (as opposed to the other way around) but I need to think through if this is more or less confusing.) In any case, I'll compose examples for each of these scenarios and see what will be reasonable to suggest. Hopefully next week. I'll comment here when I have something |
@acco Hi have you come up with anything that works now? |
Hi @crazyyi Thanks for pinging me :) Full intention to do this still. I'll post here when I make time for it |
Hey all — Thanks for your patience! I pushed a solution along with a quick write-up to I still need to write the blog post that builds off the previous one, describing the steps for readying that repo for deployment. In the meantime, the diff is highly informative. Notably:
Outside of that, I just did some juggling with the Hope this helps! |
@acco Is there any reason why you have decided to run the server in production with babel-node? Babel docs recommends against this. |
@faurehu There isn't other than to keep things simple :) Indeed, transpiling |
Another, IMHO easier way to make this deployable on heroku without buildpack.
Works like a charm, builds the project on heroku, not locally. |
For folks still looking to run create-react-app and express together on heroku, I wrote this article that covers how we are doing it at the moment. |
For anyone considering deployment on Heroku with separate instances for server and client and using cookies, keep in mind that https://devcenter.heroku.com/articles/cookies-and-herokuapp-com A custom domain url bypasses the issue, depending on DNS setup. |
Is there anything actionable for us in this issue? Perhaps, somebody would like to add something to the Heroku deployment section in User Guide? |
Hello @acco ! Since it's been about 7 months since you posted that implementation (and things move so fast in the react community :-) ), do you still use this implementation or has your perspective changed on this? Is it still good practice to remove "/build" from the .gitignore in the interest of preserving simplicity when deploying to Heroku? Let me know. |
Because of issues around the build process on Heroku, I've decided to run the build process locally and push to Heroku. This approach was recommended as per this discussion: facebook/create-react-app#639 I might revise this approach in the future.
hey guys, looking through this I'm still struggling a bit to run it the way I need:
Particularly, I want my client-server to make requests for user data to my api-server on all routes. Is this possible..? |
I'll close as I don't see an action item here for us. |
GLAH!!!!! BWAHHH! (thats a happy scream) Sorry, I was stuck on my own slightly different problem with this forever!!! Thank you @vrk and @CaliAlec for this issue. While slightly different, fixing my scripts to this based off of your responses helped me fix my issue!:
Also, I used es6 for the path for a slightly cleaner look. Thank you! I hope if there is anyone else with my problem they see this post and see my fix (or yours!) |
If anyone is trying to figure out how to do this with a Rails backend, I suggest checking out this blog post: ReactJS + Ruby on Rails API + Heroku App Seems like the Rails version of this has been neglected. 😞 Maybe a deployment section can also be added to the blog post and readme for others to find. |
Hey folks,
Thanks for create-react-app! I'm new to the world of React and overall I've had a breeze getting set up. I'm a bit puzzled on deployment still, so I thought I'd open an issue to clarify these instructions in the How-to recipes section as suggested here.
Summary:
But I can't get 1 and 2 working together, i.e. a react client + node server working on a single Heroku app.
Github repo that repros the issue:
This is a clone of the Integrating with a node backend example with a few changes to get it to partially work on Heroku, explained in detail in the README.
Questions:
Would appreciate any help. Thanks!
The text was updated successfully, but these errors were encountered: