Running a Node.js application (such as a React app) on an IIS server involves a few steps. Here's a general guide to help you get started:
Step 1: Install Node.js
Ensure that Node.js is installed on your server. You can download it from Node.js official website.
Step 2: Install URL Rewrite Module
Download and install the IIS URL Rewrite Module if you haven't already.
Step 3: Install IISNode
IISNode is an open-source project that allows you to run Node.js applications on IIS. You can download and install IISNode from GitHub.
Step 4: Set Up Your Node.js Application
Build your React application:
npm run build
Create a web.config
file in your React app's build directory with the following content:
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<rule name="StaticContent">
<action type="Rewrite" url="public/{R:0}" />
</rule>
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
</conditions>
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin" />
</hiddenSegments>
</requestFiltering>
</security>
<httpErrors existingResponse="PassThrough" />
<iisnode watchedFiles="web.config;*.js" />
</system.webServer>
</configuration>
Create a server.js
file to serve your React app. Example:
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Step 5: Deploy Your Application to IIS
- Copy your build directory (including
web.config
and server.js
) to your IIS server.
- Open IIS Manager and create a new site or use an existing site.
- Set the Physical Path to the location where you copied your build directory.
- Configure the site to use the proper binding and port.
Step 6: Configure the Application Pool
- Open IIS Manager, select your site, and open the Advanced Settings.
- Set the Application Pool to use No Managed Code.
Step 7: Start the Application
- Start your site from IIS Manager.
- Open a browser and navigate to your site. Your React application should be served by IIS through Node.js.
This setup ensures your Node.js application is correctly integrated with IIS and serves your React app effectively.