Coder Social home page Coder Social logo

cra-with-expressjsapi's Introduction

create-react-app React Project with Node Express Backend

Example on using create-react-app with a Node Express Backend

Usage

Install nodemon globally

npm i nodemon -g

Install server and client dependencies

yarn
cd client
yarn

To start the server and client at the same time

yarn dev

or

npm run dev

How this works

The key to use an Express backend with a project created with create-react-app is on using a proxy. We have a proxy entry in client/package.json

"proxy": "http://localhost:5000/"

This tells Webpack development server to proxy our API requests to our API server, given that our Express server is running on localhost:5000

-Esau

Additional

That is correct, only for development. For production you would need to run yarn build inside the client directory, then CRA will create a build directory with the production build. Back to the server.js file, require path and add the following line

app.use(‘/’, express.static(${__dirname}/client/build)); Take a look at this repo for reference, in this example I show how to serve static files with node, which is basically what you need.

Now to deploy to Heroku you need to take several steps, Stephen Grider does an excellent job in this video detailing the process on deploying to Heroku.

I should add a “Production” section to the article…

SCSS in client dir

npm-run-all will run the node-sass-chokidar This will output a css file next to the .scss file Include the .css in your bundle and the styles print inline in your app.

You can alternatively exclude that css from your .js and load it in the HTML or you can use the create-react-app directions for modules to load things dynamically.

Pretty much all majot views will be Async

how to test production

  1. navigate to /client
  2. run npm run build
  3. navigate back to root dir
  4. change the NODE_ENV property in .env to anything but "development"
  5. run npm run server

Now you will see code-splitting and production assets in action

Can I do this on Heroku?

Can I have the procfile startup the server then run yarn build??

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.