-
Initialize Node.js in the directory: This will create a
package.jsonfile, which keeps track of your dependencies.npm init -y
-
Install Express: Express is a lightweight framework for handling HTTP requests, which you'll use for your backend.
npm install express
-
Create your
server.jsfile: Create a new file calledserver.jsfor your backend code.touch server.js
-
Create the React app: Use the following command to create a React app named
clientinside your project directory.npx create-react-app client
-
Navigate into the client directory:
cd client -
Start the React app: Run the React development server.
npm start
Now we need to ensure that React can communicate with the Node.js backend.
-
Set up a proxy: Open
client/package.jsonand add the following line under"scripts":"proxy": "http://localhost:5000",
-
Modify the React app to fetch data from the backend:
Open
client/src/App.jsand modify it to fetch a message from the backend:import React, { useState, useEffect } from 'react'; function App() { const [message, setMessage] = useState(''); useEffect(() => { fetch('/api/message') .then((res) => res.json()) .then((data) => setMessage(data.message)); }, []); return ( <div className="App"> <h1>{message}</h1> </div> ); } export default App;
-
Update
server.jsto send JSON data: Modify your backend (server.js) to respond with a message when the/api/messageendpoint is hit:app.get('/api/message', (req, res) => { res.json({ message: 'Welcome to the Repair Video Showcase!' }); });
-
Restart both the backend and frontend servers:
-
In the backend, run:
node server.js
-
In the frontend (inside the
clientfolder), run:npm start
-
-
Open your browser and visit
http://localhost:3000. You should now see the message "Welcome to the Repair Video Showcase!" on your React page.
npm install axios npm install react-router-dom