Node.js 部署至 IIS 站台
基於公司網路架構的規定
記錄一下自己是如何成功將手邊的 Node.js 部署到 iis 上。
一、安裝工具
以下三樣,順序固定
.安裝 node-v14.17.5-x64.msi 的node工具
https://drive.google.com/file/d/1oMVvy0p4ViP7v-EMA8vPqIghy_ulW_Af/view?usp=sharing
.安裝 iis 的 rewrite_amd64_en.US.msi
https://drive.google.com/file/d/1O1J-NuiGpNywGesuqCeSEcFSfjKluFzN/view?usp=sharing
.安裝 iisnode-full-v0.2.21-x64.msi
https://drive.google.com/file/d/1ijIW1Zz4rXrkW8F-aJQ5AA4vDromFfog/view?usp=sharing
二、建立IIS站
.首先建立「應用程式集」
-識別:NetworkService
三、建立 web.config
.兩個方式
-自己為站台的情形,貼上以下內容
<configuration>
<system.webServer>
<!-- indicates that the server.js file is a node.js application to be handled by the iisnode module -->
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode" />
</handlers>
<rewrite>
<rules>
<rule name="sendToNode">
<match url="/*" />
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
-自己為子站台的情形,貼上以下內容
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode" resourceType="Unspecified" requireAccess="Script" />
</handlers>
<rewrite>
<rules>
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^app.js\/debug[\/]?" />
</rule>
<rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
<match url="iisnode"/>
</rule>
<rule name="StaticContent" patternSyntax="Wildcard">
<action type="Rewrite" url="public/{R:0}" logRewrittenUrl="true"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
</conditions>
<match url="*.*"/>
</rule>
<rule name="DynamicContent">
<conditions>
<add input="{{REQUEST_FILENAME}}" matchType="IsFile" negate="True" />
</conditions>
<action type="Rewrite" url="server.js" />
</rule>
</rules>
</rewrite>
<iisnode promoteServerVars="REMOTE_ADDR" watchedFiles="*.js;node_modules\*;routes\*.js;views\*.jade"/>
</system.webServer>
</configuration>
四、建立 server.js
var express = require('express');
var app = express();
var options = {
index: 'index.html'
};
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use('/你的子站台名稱', express.static('_site', options));
app.set('trust proxy', 1);
app.get('/你的子站台名稱', function (req, res) {
res.send('Express is working on IISNode!');
});
var http = require('http');
var port =process.env.PORT;
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
五、運行IIS,就能看見基礎網頁了。
留言
張貼留言