How to run some server-side JS then start a react app - javascript

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.

Related

Rendering react with node.js in development

I am (very) new to node.js and I am trying to get a development environment started with React.
const express = require('express')
const mongoose = require('mongoose')
const app = express()
// this displays the index.html file
app.use(express.static(__dirname + '/public'));
// trying to see the app.js
app.get('/', (req, res) => {
res.render('app')
})
app.listen(process.env.PORT || 5000);
Right now I am simply trying to be able to view my app.js when I run nodemon. But right now it is only showing the index.html file in the public folder. and when I run npm start it renders both the index.html and the app.js.
I am 100% doing something wrong and am not in my element here. Any help/advice would be appreciated.
My full repo is here for viewing here
Thank you in advance.
Your code is simply serving a static HTML file located in the public directory everytime the user make a GET request to the root (in your case, localhost:5000). It is not interacting with React yet.
Usually when starting a project with React (frontend) and Node (backend), you would create them as separate projects, in separate repositories.
So you could create a React application using a bootstrap such as create-react-app running on PORT 3000 and in another terminal tab, start your NodeJS application in PORT 5000 like in your example. Then, you can call your backend endpoint from your frontend React application, by referencing http://localhost:5000
By doing this, your backend code don't need to serve static files anymore, it can return data such as JSON and make connections to a database for example.
Since your question is not specific enough, you could be talking about server side render. In server side render apps using Node and React, you have a frontend built with React and a Node server that will return the same React code as a string, using the react-dom/server package to help. This is more complex, but basically you will have the same React code on the client AND on the server. There are performance benefits, because the user can see some content rendered right when he enters the page, without having to wait the React bundle (javascript file) to load. If you want to know more about creating a server side render app with React and Node, there is a nice digital ocean tutorial here
If you want to connect your react js project to the node js
In Development
you simply run their server and start it
In Production
You can use sendFile function coming from express.js to run the frontend
https://dev.to/loujaybee/using-create-react-app-with-express

How to implement Socket.io with next.js without separate node server

I want to build a chat system with Next.js. I am not sure how I have to setup the socket server.
I want this to implement Next server not with a separate server.
Should I have to make a server.js file in root and run a server?
You will need to create custom server however, then you can't deploy to vercel, and you got yourself a regular nodejs application.
You could use cloud providers to handle sockets for you, or as you said, you could split your application to a regular next.js app and deploy your socket application separately.

How to handle backend API request for create-react-app bootstrapped app which is build and ran using "serve-s build" on AWS ec2?

I have create-react-app bootstrapped application which I build and then serve it to the static server using serve-s build. The React app is running on http://locahost:3000.
This app talks to my backend REST API(using java) which is running on http://locahost:8080.
Inside my React application, I have set axios.default.baseURL=http://localhost:8085/api.
Everything is fine on the localhost on my system. The React app talks to http://localhost:8085/api/xyz for CRUD operations and everything works great.
Now I have to deploy the project on AWS EC2 instance. The MySQL and REST API got deployed and rest API there is running on same instance on the port 8085 as http://locahost:8085. I have Apache server configured which sends the hostname(www.myxyzwebsite.com) to the http://locahost:3000 on the server. Everything is fine till now. The home page is visible on the browser.
Now, when the React app tries to communicate to the REST API from the browser, it is sending the request to the http://locahost:8085. Obviously, now the browser looks for some service running on port 8085 on my system and it couldn't find. Ideally, the request should go to the server with my hostname(www.myxyzwebsite.com/api/users/puspen). How to make this REST API call looks like an actual call like www.myxyzwebsite.com/users/pusp?
NOTE: Please note that this is not a server-side-rendered application.

react native: how to automatically run websocket server when app starts

i've integrated socket.io into my react native app and it works successfully. However i always have to start the server manually using nodemon. After running react-native run-android i also have to enter nodemon index.js from the terminal which contains my socket.io configuration in order to get the server working. I need this server to start automatically whenever the app starts since i plan to build the app and run it on a real device where i cannot execute the nodemon command in a terminal to start the websocket server

Could not connect to development server running React Native even though development server receives ping

This is for React Native on Android.
When I try to start my React Native screen in my app I get the red screen that says Could not connect to development server. However this can't be true. Every time I try to reload the Javascript I see the following update in the terminal that's running my react native server:
Bundling `index.android.js`
Transforming modules 100.0% (390/390), done.
Bundling `index.android.js`
No module changed.
Bundling `index.android.js`
No module changed.
Clearly it's making a request to the server, but the error says something else. What's going on here?

Categories