
Introduction
Deploying full-stack applications can seem daunting, especially when you’re working with a Node.js backend and a React.js frontend. Fortunately, Netlify simplifies the process, allowing you to host both parts of your application seamlessly. In this guide, we’ll walk through deploying a full-stack Node.js and React application on Netlify step by step.
Prerequisites
Before we begin, make sure you have:
- A full-stack project with Node.js as the backend and React as the frontend.
- A GitHub, GitLab, or Bitbucket repository for your code.
- A Netlify account (you can sign up at Netlify).
- Basic knowledge of JavaScript and deployment processes.
Step 1: Prepare Your Project Structure
Ensure your project has the following structure:
root/
|-- client/ (React frontend)
|-- server/ (Node.js backend)
|-- package.json
|-- netlify.toml
- client/: Contains your React application.
- server/: Contains your Node.js application.
- package.json: Includes scripts for building and running your application.
- netlify.toml: Configuration file for Netlify.
Step 2: Configure Your Backend for Serverless Functions
Netlify uses serverless functions to run backend code. Move your Node.js server logic into a folder named functions
:
- Create a
functions
folder in your project’s root directory. - Move your existing server files (e.g.,
server.js
) into thefunctions
folder. - Update your
server.js
to follow the serverless function format:
exports.handler = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({ message: "Hello from the backend!" }),
};
};
- Add dependencies required for your backend in the root
package.json
file.
Step 3: Build and Optimize Your Frontend
Navigate to the client/
directory and build your React app:
cd client
npm run build
This generates a build
folder containing static assets for your frontend.
Step 4: Configure the netlify.toml
File
Add a netlify.toml
file to the root of your project with the following configuration:
[build]
publish = "client/build"
command = "npm install && npm run build"
[[functions]]
directory = "functions"
This tells Netlify to:
- Publish static files from the React
build
folder. - Use the
functions
directory for serverless functions.
Step 5: Push Your Code to GitHub
Ensure your project is version-controlled and pushed to a GitHub repository:
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin <your-repo-url>
git push -u origin main
Step 6: Deploy to Netlify
- Log in to your Netlify account.
- Click New site from Git.
- Connect your GitHub repository.
- Configure the deployment settings:
- Build Command:
npm run build
- Publish Directory:
client/build
- Functions Directory:
functions
- Build Command:
- Click Deploy site.
Netlify will build and deploy your application.
Step 7: Test Your Deployment
After deployment, Netlify provides a live URL for your application. Verify that:
- The React frontend loads correctly.
- Backend routes (serverless functions) work as expected.
Step 8: Set Up Environment Variables
If your application uses environment variables:
- Navigate to your site’s settings in Netlify.
- Go to Site settings > Environment variables.
- Add the required variables.
Redeploy the site to apply the changes.