Hosting a React frontend with a Node.js/Express backend on IIS involves several steps, including configuring IIS to handle Node.js applications, setting up your React app for production, and ensuring secure and private access. Below is a comprehensive guide to help you deploy your project on a new Windows server using IIS Manager.
Prerequisites
- Windows Server: Ensure you have administrative access to the Windows server where you intend to host your application.
- Node.js Installed: Download and install the latest LTS version of Node.js on the server.
- IIS Installed: Ensure that Internet Information Services (IIS) is installed on your server. You can add it via "Server Manager" > "Add Roles and Features."
Step 1: Install Required IIS Modules
To run Node.js applications on IIS, you need to install iisnode and URL Rewrite Module.
-
Install iisnode:
- Download the latest iisnode MSI installer compatible with your server's architecture (x86 or x64).
- Run the installer and follow the prompts to complete the installation.
-
Install URL Rewrite Module:
- Download the URL Rewrite Module from the official IIS website.
- Run the installer and complete the installation.
Step 2: Prepare Your React Application
-
Build the React App for Production:
- On your development machine, navigate to your React project directory.
- Run the build command:
npm run build
- This will generate a
build folder containing optimized static files.
-
Transfer Build Files to Server:
- Copy the
build folder and your backend (Node.js/Express) application files to the server. You can place them in a directory like C:\inetpub\wwwroot\YourApp.
Step 3: Configure Your Node.js/Express Backend
-
Modify Server Code to Listen on a Specific Port:
-
Ensure Environment Variables Are Managed Properly:
- iisnode sets the
PORT environment variable, so ensure your application can utilize it as shown above.
Step 4: Configure IIS to Host the Application
-
Open IIS Manager:
- Press
Win + R, type inetmgr, and press Enter.
-
Add a New Website:
- Right-click on "Sites" in the Connections pane and select Add Website.
- Site Name: Enter a name for your site (e.g.,
YourApp).
- Physical Path: Browse to the directory where you placed your application (e.g.,
C:\inetpub\wwwroot\YourApp).
- Binding: Set the appropriate IP address, port (typically 80 for HTTP or 443 for HTTPS), and host name if necessary.
- Click OK to create the site.
-
Configure web.config:
-
In your application directory (C:\inetpub\wwwroot\YourApp), create a web.config file if it doesn't exist.
-
Add the following configuration to handle both the React frontend and Node.js backend:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<!-- Serve React static files -->
<rewrite>
<rules>
<!-- Redirect API calls to Node.js backend -->
<rule name="API" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^api/(.*)" />
<action type="Rewrite" url="http://localhost:3000/api/{R:1}" />
</rule>
<!-- Serve static files for React app -->
<rule name="React Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
<!-- Handle Node.js (iisnode) -->
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>
<!-- Default document -->
<defaultDocument>
<files>
<clear />
<add value="index.html" />
</files>
</defaultDocument>
<!-- Enable static content -->
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
</system.webServer>
</configuration>
Notes:
- Replace
server.js with the entry point of your Node.js application if it's different.
- The above configuration assumes:
- API endpoints are prefixed with .
Step 5: Serve React's Static Files via IIS
-
Point IIS to the React build Directory:
- Ensure that the
index.html and other static assets from the React build are accessible.
- In the
web.config above, the defaultDocument is set to index.html, and any requests not matching static files or API routes are redirected to index.html.
-
Set Up Frontend Routing (If Using React Router):
- The rewrite rules in
web.config handle client-side routing by redirecting all non-API and non-file requests to index.html.
Step 6: Test Your Deployment
-
Start the IIS Site:
- In IIS Manager, ensure your site is started.
-
Access the Application:
- Open a web browser and navigate to
http://your-server-ip or the configured domain.
- Verify that:
- The React frontend loads correctly.
- API endpoints are accessible and functioning as expected.
-
Troubleshoot Common Issues:
- Permission Issues: Ensure the IIS user (
IIS_IUSRS) has read and execute permissions on your application directory.
- Port Conflicts: Confirm that the ports used by your Node.js application are not blocked by firewalls or used by other applications.
- Logging: Use
iisnode logging to diagnose issues. Logs are typically located in the logs directory within your application folder or as configured.
Step 7: Secure Your Application
-
Enable HTTPS:
- Obtain an SSL certificate and configure HTTPS bindings in IIS to ensure encrypted traffic.
-
Firewall Configuration:
- Configure the Windows Firewall to allow traffic only on necessary ports (e.g., 80 for HTTP and 443 for HTTPS).
- If keeping the project private, consider restricting access to specific IP addresses or using VPNs.
-
Authentication and Authorization:
- Implement authentication mechanisms if your application requires user access control.
- Configure IIS authentication settings as needed.
-
Protect Sensitive Data:
- Ensure that environment variables and sensitive configurations are not exposed.
- Use secure methods to handle secrets and credentials.
Additional Tips
-
Process Management:
- While
iisnode manages your Node.js process, consider monitoring your application using tools like PM2 for better process management and automatic restarts in case of failures.
-
Logging and Monitoring:
- Implement comprehensive logging for both frontend and backend to monitor application health and debug issues.
- Utilize IIS logs and your Node.js application's logging system.
-
Continuous Deployment:
- For streamlined deployments, consider setting up CI/CD pipelines that automate the build and deployment process to the server.
Resources
By following these steps, you should be able to successfully deploy your React and Node.js application on IIS, ensuring it's securely hosted on your private server. If you encounter specific issues during deployment, feel free to provide error messages or logs for more targeted assistance.