This is the one server implementation of the simple login app (MERN stack), where both frontend and backend are hosted on the same server.
There are 2 node directories:
- root folder
client
folder
Go into root folder and run npm install
.
Then go into client/
folder and run npm install
There are 2 .env
files:
- one
.env
file to be created in the root folder with the following content:
CLIENT_URI=http://localhost:5000
MONGO_URI=
SESSION_SECRET=
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
- another
.env
is already in theclient
folder with the content:
REACT_APP_BACKEND_URI=http://localhost:5000/api
Create that .env
file in the root folder and fill in the parameters with appropriate values.
Because we're going to host both the frontend and the backend on the same server, we need to turn those React files into simple static files that we can serve from the server.
Go into client/
folder and run npm run build
.
This will populate the client/build/
folder with static files of the frontend.
Go into the root folder and run npm start
.
This will start the site on http://localhost:5000
Later if we make changes to the frontend, ie within the client/
folder, be sure to then run npm run build
so that the client/build/
folder will have the latest static files.
Before deploying to heroku, be sure to update the .env
file within the client/
folder so that it points to the site URI, and then run npm run build
.