0

I'm wondering how to run some server side JS which then starts a react app. Or if there is a way to explicitly segment sever vs client side JS, or do I have to just add and use Express to the react app?

Essentially I want to

node somefile.js
//do some server js stuff, then

npm run start

For example, if I wanted to check a users authentication status from a separate Next app before starting and serving a Docusaurus app.

Bryan
  • 143
  • 1
  • 10
  • Sounds kind of odd to use server-side code to conditionally start an app based on authentication in a client-side environment. What happens when more than one person attempts to do this at the same time? – Heretic Monkey Dec 14 '22 at 15:53
  • ah didn't think about it that way. Considering that, if I want to do some server side things with a react app I should be adding and using Express? Is it possible to not use express? And I'd still be curious to know how to have some js files in an app run server side and others run client side? or am I missing some fundamental concept? – Bryan Dec 14 '22 at 16:04
  • Yes. Web applications work by the client making requests to the server, which responds in kind (there are exceptions, but in general this holds true). So no, you should not add express to a react app. Your react app should make a request to a separate express app, that would then do what it needs to do based on the information provided with the request. Or skip express and use some other request-processing framework in whatever language you want. – Heretic Monkey Dec 14 '22 at 16:07
  • what if my request is to render the react app itself? or am I just describing a use case for SSR? Also, isn't NextJS essentially a react app with a built in server? Confused by your comment to not add express to a react app. Thanks! – Bryan Dec 14 '22 at 16:15
  • You asked to not use express. And you shouldn't, because React runs in a client-side environment, and express will fail to run in that environment. NextJS essentially recreates React on the server so that it can render to files/memory for caching. I think you need to do a bit more research into [the difference between server-side and client-side](https://stackoverflow.com/q/1404376/215552) – Heretic Monkey Dec 14 '22 at 16:24
  • Thanks monkey, yes doing research. I think this discussion on Next's github reiterates your comment https://github.com/vercel/next.js/issues/404 – Bryan Dec 14 '22 at 16:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250419/discussion-between-bryan-zawlocki-and-heretic-monkey). – Bryan Dec 14 '22 at 16:50

0 Answers0