IIS部署Node.js项目
安装node
直接在node官网下载就可以,查看是否安装成功可以node -v试一下
安装插件iisnode
下载地址,根据需求下载即可
下载之后双击打开程序包安装,默认安装位置是在C:\Program Files\iisnode
在此文件夹下运行setupsamples.bat,可能会遇到拒绝访问的问题
这里解决方案就是管理员运行就可以,因为已经安装过了,安装成功的截图就不展示了,安装之后打开iis会看到默认站点下多了一个node应用程序
浏览一下访问http://localhost/node/出现以下画面就是成功
安装iis的URL Rewrite模块
点击下载URL重写模块,进去划到最下面下载需要的版本
下载后也是程序包,双击打开进行安装就可以
在iis看到这个就是安装成功了,ok,接下来就可以部署项目
部署node项目
部署项目和平常一样新建站点然后物理路径指向项目所在目录就可以,重点来了,在根目录下添加一个web.config文件,内容如下
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="run.js" verb="*" modules="iisnode" resourceType="Unspecified" requireAccess="Script" preCondition="bitness64" />
</handlers>
<rewrite>
<rules>
<rule name="all">
<match url="/*" />
<action type="Rewrite" url="run.js" />
</rule>
</rules>
</rewrite>
<iisnode node_env="%node_env%"
nodeProcessCountPerApplication="1"
maxConcurrentRequestsPerProcess="1024"
maxNamedPipeConnectionRetry="100"
namedPipeConnectionRetryDelay="250"
maxNamedPipeConnectionPoolSize="512"
maxNamedPipePooledConnectionAge="30000"
asyncCompletionThreadCount="0"
initialRequestBufferSize="4096"
maxRequestBufferSize="65536"
watchedFiles="*.js;node_modules\*;routes\*.js;views\*.jade"
uncFileChangesPollingInterval="5000"
gracefulShutdownTimeout="60000"
loggingEnabled="true"
logDirectory="iisnode"
debuggingEnabled="true"
debugHeaderEnabled="false"
debuggerPortRange="5058-6058"
debuggerPathSegment="debug"
maxLogFileSizeInKB="128"
maxTotalLogFileSizeInKB="1024"
maxLogFiles="20"
devErrorsEnabled="true"
flushResponse="false"
enableXFF="false"
configOverrides="iisnode.yml"
nodeProcessCommandLine="C:\Program Files\nodejs\node.exe"
promoteServerVars="REMOTE_ADDR" />
<defaultDocument>
<files>
<add value="run.js" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
rewrite重写url指向项目入口文件,也就是上边的run.js,指向入口文件之后基本流程就完成了,可能项目中的入口文件端口还需要改一下
这时候,问题来了,使用egg框架生成的一套项目没有像app.js或者index.js这样的入口文件,运行命令直接就是npm run dev,所以部署的时候就没办法在这里指定一个入口文件,查看官网有一段内容是启动自定义,但只是在运行前做一些动作,并不能严格的来说是入口文件
iis部署egg.js
其实到最后还是要自己手写一个入口文件的
const egg = require('egg');
function normalizePort(val) {
const listenPort = parseInt(val, 10);
if (isNaN(listenPort)) {
return val;
}
if (listenPort >= 0) {
return listenPort;
}
return false;
}
const port = normalizePort(process.env.PORT)||7200;
egg.start({ ignoreWarning: true })
.then(app => {
app.listen(port);
app.logger.info(`server running on ${port} ...`);
});
const port = normalizePort(process.env.PORT)||7200;这里就是指定项目运行之后的端口号,以上就是web.config中指向的run.js内容,相比于普通部署这也是唯一的不同,接下来直接访问接口就可以