IISにNext.jsがデプロイできるという事で、React Router v7でも試してみた。
参考サイト
https://qiita.com/utsubo-daisuki/items/1b48dd69668dc70d2f2a
https://nodejs.keicode.com/nodejs/using-platformhandler-to-host-nodejs-on-iis.php
概要
IIS拡張モジュール「HttpPlatformHandler」を使うと、IISからnode.exeを起動してHTTP要求をNodeプロセスに処理させることができる。
NodeプロセスでReact Routerを起動させる設定をすればOK。
HttpPlatformHandler インストール
https://www.iis.net/downloads/microsoft/httpplatformhandler
からダウンロード(httpPlatformHandler_amd64.msiなど)して実行する。
React Router 側の設定
今回はbasedirを / にするので特に設定なし。
以下のコマンドでビルドした。
pnpm run build
IISの設定
C:\projects\rr に、上記で作成したbuildフォルダとpackage.jsonを配置。
(build内にはclientとserverフォルダがある)
最初は pnpm install したが、node_module内のハードリンクをIISの実行ユーザでうまく読み込めずエラーとなってしまったため、npm install で node_moduleフォルダを作成した。
また、IISのWebサイトの物理パスを C:\projects\rr に変更した。
web.config
C:\projects\rr フォルダ内にweb.configファイルを作成し、以下の内容とした。
この後、IISのサービスを再起動した。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add
name="httpPlatformHandler"
path="*"
verb="*"
modules="httpPlatformHandler"
resourceType="Unspecified" />
</handlers>
<httpPlatform
processPath="C:\Programs\nodejs\node.exe"
arguments="C:\projects\rr\node_modules\@react-router\serve\dist\cli.js C:\projects\rr\build\server\index.js"
stdoutLogEnabled="true"
stdoutLogFile="C:\projects\rr\logs\stdout">
<environmentVariables>
<environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
</environmentVariables>
</httpPlatform>
</system.webServer>
</configuration>
実行確認
ブラウザで、下記にアクセス。
http://localhost/
React Routerの画面が表示されれば成功!