Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support iis server preset #1436

Merged
merged 6 commits into from Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/content/2.deploy/providers/iis.md
@@ -0,0 +1,26 @@
# IIS

Deploy Nitro apps to IIS.

::alert{type="warning"}
This is an experimental preset.
::

## Using [IISnode](https://github.com/Azure/iisnode) (recommended)

**Preset:** `iis-node` ([switch to this preset](/deploy/#changing-the-deployment-preset))

1. Install [IISnode](https://github.com/azure/iisnode/releases), and the [IIS URL Rewrite Module](https://www.iis.net/downloads/microsoft/url-rewrite).
2. In IIS, add `.mjs` as a new mime type and set its content type to `application/javascript`.
3. Deploy the contents of your `.output` folder to your website in IIS.


## Using IIS directly

**Preset:** `iis-handler` ([switch to this preset](/deploy/#changing-the-deployment-preset))

If you do not wish to use IISnode, you can use IIS directly.

1. Make sure that [Node.js](https://nodejs.org/en/) is installed on your Windows Server.
2. Make sure [`HttpPlatformHandler` Module](https://www.iis.net/downloads/microsoft/httpplatformhandler) is installed.
3. Copy your `.output` directory into the Windows Server, and create a website on IIS pointing to that exact directory.
65 changes: 65 additions & 0 deletions src/presets/iis-node.ts
@@ -0,0 +1,65 @@
import { resolve } from "pathe";
import { writeFile } from "../utils";
import { defineNitroPreset } from "../preset";
import type { Nitro } from "../types";

export const iisNode = defineNitroPreset({
extends: "node-server",
hooks: {
async compiled(nitro: Nitro) {
await writeFile(
resolve(nitro.options.output.dir, "web.config"),
iisnodeXmlTemplate()
);

await writeFile(
resolve(nitro.options.output.dir, "index.js"),
"import('./server/index.mjs');"
);
},

Check warning on line 19 in src/presets/iis-node.ts

View check run for this annotation

Codecov / codecov/patch

src/presets/iis-node.ts#L10-L19

Added lines #L10 - L19 were not covered by tests
},
});

function iisnodeXmlTemplate() {
return `<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<webSocket enabled="false" />
<handlers>
<add name="iisnode" path="index.js" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^server\\/debug[\\/]?" />
</rule>

<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>

<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="index.js"/>
</rule>
</rules>
</rewrite>

<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin"/>
<add segment="node_modules"/>
</hiddenSegments>
</requestFiltering>
</security>

<httpErrors existingResponse="PassThrough" />

<iisnode watchedFiles="web.config;*.js" node_env="production" debuggingEnabled="true" />
</system.webServer>
</configuration>
`;
}

Check warning on line 65 in src/presets/iis-node.ts

View check run for this annotation

Codecov / codecov/patch

src/presets/iis-node.ts#L23-L65

Added lines #L23 - L65 were not covered by tests
34 changes: 34 additions & 0 deletions src/presets/iis.ts
@@ -0,0 +1,34 @@
import { resolve } from "pathe";
import { writeFile } from "../utils";
import { defineNitroPreset } from "../preset";
import type { Nitro } from "../types";

export const iis = defineNitroPreset({
extends: "node-server",
hooks: {
async compiled(nitro: Nitro) {
await writeFile(
resolve(nitro.options.output.dir, "web.config"),
iisXmlTemplate()
);
},

Check warning on line 14 in src/presets/iis.ts

View check run for this annotation

Codecov / codecov/patch

src/presets/iis.ts#L10-L14

Added lines #L10 - L14 were not covered by tests
},
});

function iisXmlTemplate() {
return `<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script" />
</handlers>
<httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\\logs\\node.log" startupTimeLimit="20" processPath="C:\\Program Files\\nodejs\\node.exe" arguments=".\\server\\index.mjs">
<environmentVariables>
<environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
<environmentVariable name="NODE_ENV" value="Production" />
</environmentVariables>
</httpPlatform>
</system.webServer>
</configuration>
`;
}

Check warning on line 34 in src/presets/iis.ts

View check run for this annotation

Codecov / codecov/patch

src/presets/iis.ts#L18-L34

Added lines #L18 - L34 were not covered by tests
2 changes: 2 additions & 0 deletions src/presets/index.ts
Expand Up @@ -26,5 +26,7 @@ export * from "./cleavr";
export * from "./layer0";
export * from "./flightcontrol";
export * from "./lagon";
export * from "./iis-node";
export * from "./iis";
export { _static as static } from "./static";
export * from "./github-pages";