前言:最近因为项目原因,需要在IIS下部署node项目,在此之前,曾经部署过类似的项目,因此在这次部署还算比较顺利,只是在其中遇到了几个比较坑的问题,所以这次使用博客记录下来,如有园友遇到过类似问题,希望对你有所帮助。
一、前期准备
1、node.js(下载地址:https://nodejs.org/en/),根据自己的需要安装对应版本
2、iisnode(下载地址:https://github.com/tjanczuk/iisnode)
3、IIS的URL Rewrite模块(下载地址:https://www.iis.net/downloads/microsoft/url-rewrite)
依次安装好以上软件,记录下node.js的安装路径(例如我的安装路径是:C:\software\nodejs),在后续中会用到。
如果需要测试iisnode是否安装成功,可以用
%programfiles%\iisnode\setupsamples.bat
执行后:
来安装iisnode自带的一个例子,安装完成后,访问:http://localhost/node,如果网页不能编译出现以下错误的解决办法:
The iisnode module is unable to start the node.exe process. Make sure the node.exe executable is available at the location specified in the system.webServer/iisnode/@nodeProcessCommandLine element of web.config. By default node.exe is expected in one of the directories listed in the PATH environment variable
在要打开在页面所在文件夹下的web.config中添加以下内容:
nodeProcessCommandLine=""%programfiles%\nodejs\node.exe"" interceptor=""%programfiles%\iisnode\interceptor.js"" />,注意配置文件里只允许有一个 iisnode 属性设置
如图
二、部署项目
在IIS下新建一个站点(此过程不在赘述),然后在项目下打开控制台,安装项目需要依赖的包,执行:
npm install
安装完成后会在项目中新增一个文件夹:
然后编辑web.config:
<configuration> <system.webServer> <!-- bin/www 是Express示例默认的启动程序 --> <handlers> <add name="iisnode" path="bin/www" verb=