0

I understand that the normal use case for a React/Express app is to run one dev server for your Express app, and one for your React app. My question is, is it at all possible to run the front-end dev server within an Express app, such that:

  • I can start both with node index.js
  • (optional) the two servers share a common port

In other words, is there any way to programmatically start a Webpack dev server from within Node/Javascript (with JS; not using something like execute to run npm start), and if so is there anything special about the create-react-app dev server that would prevent running it programmatically?

EDIT: I found https://github.com/webpack/webpack-dev-middleware, which is almost what I'm looking for. However, it's Webpack, not create-react-app, so while it does allow for hot reloading, it doesn't preserve state between reloads (the way create-react-app's version of Webpack's server does).

So it seems I'm really looking for a create-react-app-dev-server-middleware ... or a way to configure the webpack-dev-middleware to support create-react-app's state preservation between hot reloads.

machineghost
  • 33,529
  • 30
  • 159
  • 234
  • the webpack dev server is running express, you could eject your app and make whatever changes in the existing express server. though this seems like a really unusual use case. what are you actually to acheive? – azium Sep 27 '20 at 15:06
  • and sure you can always import the express app into your other node application and call `.listen()` or whatever the result of calling `npm start` does – azium Sep 27 '20 at 15:08
  • What I'm trying to achieve is a dev server that starts as a single server and (ideally) runs on a single port. There is no "live version" of this particular Express server, so I just need one server that can run arbitrary Node commands (ie. an Express server) and can hot reload React front-end files. – machineghost Sep 27 '20 at 15:25
  • And yes I understand I can change which port my Express app listens on, but I still wind up with two servers, that have to be started/restarted separately, each running on two ports. Since there is clearly a way to run the Webpack dev server (ie. the thing create-react-app uses to run its dev server) as Express middleware, it *seems* I should be able to do something similar to get the create-react-app functionality (as middleware). – machineghost Sep 27 '20 at 15:26

0 Answers0