Node.js教程 使用iisnode部署node项目遇到的坑
沉沙 2018-10-09 来源 : 阅读 3882 评论 0

摘要:本篇教程介绍了Node.js教程 使用iisnode部署node项目遇到的坑,希望阅读本篇文章以后大家有所收获,帮助大家对Node.js的理解更加深入。

本篇教程介绍了Node.js教程 使用iisnode部署node项目遇到的坑,希望阅读本篇文章以后大家有所收获,帮助大家对Node.js的理解更加深入。

<


前言:最近因为项目原因,需要在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自带的一个例子,安装完成后,访问://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="*" modules="iisnode" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="myapp">
          <match url="/*" />
          <action type="Rewrite" url="bin/www" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

此时在浏览器中打开地址//localhost/myapp/,将出现404错误,原因是bin目录是默认输出目录,默认不允许模块调用

HTTP 错误 404.8 - Not Found
 
请求筛选模块被配置为拒绝包含 hiddenSegment 节的 URL 中的路径。

解决办法:
在你的项目根目录下,新建一个index.js的文件,然后将bin/www里面的代码剪切过来,同时可以删除bin文件夹了,并且再次修改你的web.config文件,将入口程序改成index.js:

<configuration>
  <system.webServer>

    <!-- indicates that the hello.js file is a node.js application 
    to be handled by the iisnode module -->

    <handlers>
        <add name="iisnode" path="index.js" verb="*" modules="iisnode" resourceType="Unspecified" requireAccess="Script" preCondition="bitness64" />
    </handlers>
    
    <rewrite>
            <rules>
                <rule name="all">
                    <match url="/*" />
                    <action type="Rewrite" url="index.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:\software\nodejs\node.exe" 
        promoteServerVars="REMOTE_ADDR" />
        
        <defaultDocument>
            <files>
                <add value="index.js" />
            </files>
        </defaultDocument>
      
    <!--     
    
    One more setting that can be modified is the path to the node.exe executable and the interceptor:
    
    <iisnode
      nodeProcessCommandLine="&quot;%programfiles%\nodejs\node.exe&quot;" 
      interceptor="&quot;%programfiles%\iisnode\interceptor.js&quot;" />
    
    -->

  </system.webServer>
</configuration>

此时你就可以运行你的项目了,如果在运行的时候输出如下错误:

iisnode encountered an error when processing the request.

请检查你的index.js文件的require路径是否正确:

var app = require(‘./app‘);
var debug = require(‘debug‘)(‘myapp:server‘);
var http = require(‘http‘);

正常情况下,此时你的项目就可以正常运行了,但是!!!还没完!!,最坑人的地方来了,你的项目可能会一直报一下错误:

iisnode encountered an error when processing the request.

HRESULT: 0x2 
HTTP status: 500 
HTTP reason: Internal Server Error 
You are receiving this HTTP 200 response because system.webServer/iisnode/@devErrorsEnabled configuration setting is ‘true‘.

In addition to the log of stdout and stderr of the node.exe process, consider using debugging and ETW traces to further diagnose the problem.

The node.exe process has not written any information to stderr or iisnode was unable to capture this information. Frequent reason is that the iisnode module is unable to create a log file to capture stdout and stderr output from node.exe. Please check that the identity of the IIS application pool running the node.js application has read and write access permissions to the directory on the server where the node.js application is located. Alternatively you can disable logging by setting system.webServer/iisnode/@loggingEnabled element of web.config to ‘false‘.

找了很久错误,始终以为是哪个环节安装好,或者依赖包没有安装正确,始终没有解决,在快绝望的时候,终于在stackoverflow上找到了一位外国网友记录这个错误解决办法:
地址:https://stackoverflow.com/questions/24028537/iisnode-encountered-an-error-when-processing-the-request/24038377

老铁们!看到这个解决办法我也是想哭了,还有这个??于是修改了项目的user权限,问题就迎刃而解了。

三、总结
永远不要忽略细节!!!
   

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注WEB前端Node.js频道!

本文由 @沉沙 发布于职坐标。未经许可,禁止转载。
喜欢 | 3 不喜欢 | 0
看完这篇文章有何感觉?已经有3人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程