In this tutorial, we will walk through the steps to create an Express/Node + React project using Vite as the build tool. We will set up a Node backend with Express and a React frontend, and use Vite to harness the power of modern frontend development tools like hot module replacement, fast builds, and more.
Step 1: Set Up the Node Backend
- Create a new directory for your project and navigate into it:
mkdir my-express-react-project
cd my-express-react-project
- Initialize a new Node.js project:
npm init -y
- Install Express as a dependency:
npm install express
- Create a new file called
server.js
and add the following code to set up a basic Express server:
const express = require('express');
const app = express();
const port = 3001;
app.get('/', (req, res) => {
res.send('Hello, Express!');
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
- Run the server using the following command:
node server.js
You should see the message Server running at http://localhost:3001
in your terminal. Your Node backend is now up and running!
Step 2: Set Up the React Frontend
- Create a new directory called
client
inside your project directory:
mkdir client
- Navigate into the
client
directory and initialize a new React project using Create React App:
npx create-react-app .
- Install the Vite package and vite-plugin-react as dev dependencies:
npm install -D vite vite-plugin-react
- Create a new file called
vite.config.js
in the root of theclient
directory and add the following code to configure Vite to work with React:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()]
});
- Modify the
scripts
section of thepackage.json
file in theclient
directory to use Vite for development:
"scripts": {
"start": "vite",
"build": "vite build"
}
- Run the frontend using the following command:
npm start
Open http://localhost:3000 in your browser to see your React frontend running. You should see the default Create React App homepage.
Step 3: Connect the Frontend and Backend
- Update the
server.js
file to serve the React frontend:
const express = require('express');
const app = express();
const port = 3001;
app.use(express.static('client/build'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'));
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
- Build the React frontend using Vite:
cd client
npm run build
-
Restart the server by running
node server.js
in the root of your project directory. - Open http://localhost:3001 in your browser and you should see your React frontend served by the Express backend.
Congratulations! You have successfully created an Express/Node + React project with Vite. This setup combines the robustness of a Node backend with the modern frontend development tools provided by React and Vite. Feel free to customize and expand upon this project as needed for your specific requirements. Happy coding!
How can we deploy this project to Digital Ocean? Shall we dockerize it?
Bro it shows access is blocked by cors policy how to resolve this?
Thanks bro😢 I have been struggling how to connect but ur video solved me pls upload more video about full stack projects pls 😢
Damn bro, your way of explaining is soo damn good that i can understand everything you said there, thanks
Thanks, man!
thank you for this! Saved me so much time
is it possible to depoly such a project to vercel? im quite new to all of this. What would i need to do? Great Tutorial!
Beautiful job, congrats and thanks. Now its time to containerize it
Sometimes i wish i started with backend and then frontend….sheesh its a whole different world out here
Thanks brother! This really saved much of my time. Keep on producing such useful videos…..
I'm kind of new to react.
Do I need express for a single page application or the Vite server would be enough?