# Deploy Next.js Application on IIS
This project is a Next.js application configured to run on IIS using `iisnode`. It is optimized for production and includes a basic server setup.
## Table of Contents
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Configuration](#configuration)
- [Running the Application](#running-the-application)
- [Routes](#routes)
- [License](#license)
## Prerequisites
- [Node.js](https://nodejs.org/) (Ensure it's installed on your server)
- [IIS](https://www.iis.net/) (Internet Information Services)
- [iisnode](https://github.com/tjanczuk/iisnode) (Node.js integration for IIS)
## Installation
1. Clone the repository:
```bash
git clone <repository-url>
cd <repository-folder>
-
Install dependencies:
npm install
-
Install
cross-envas a development dependency:npm install --save-dev cross-env
The server.js file configures the Next.js server:
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const port = process.env.PORT || 3000;
const app = next({ dev })
const handle = app.getRequestHandler()
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
if (pathname === '/a') {
app.render(req, res, '/a', query)
} else if (pathname === '/b') {
app.render(req, res, '/b', query)
} else {
handle(req, res, parsedUrl)
}
}).listen(port, (err) => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})
})The web.config file contains the IIS configuration:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="myapp">
<match url="/*" />
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
<iisnode node_env="production" nodeProcessCommandLine=""C:\Program Files\nodejs\node.exe"" interceptor=""%programfiles%\iisnode\interceptor.js"" />
</system.webServer>
<location path="" overrideMode="Deny">
<system.webServer>
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>
</system.webServer>
</location>
</configuration>-
Build the application for production:
npm run build
-
Start the application:
npm start
This application currently supports the following routes:
/a- Render the page associated with route/a./b- Render the page associated with route/b.- Other routes are handled by the Next.js request handler.