记录一次使用iisnode部署node项目遇到的坑!

前言:最近因为项目原因,需要在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="*" modules="iisnode" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="myapp">
          <match url="/*" />
          <action type="Rewrite" url="bin/www" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

此时在浏览器中打开地址http://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权限,问题就迎刃而解了。

三、总结

永远不要忽略细节!!!

原文地址:https://www.cnblogs.com/zhao-yi/p/9190282.html

时间: 2024-08-01 03:49:30

记录一次使用iisnode部署node项目遇到的坑!的相关文章

服务器部署node项目

相信如果做过node项目,在本地应该遇到的问题不会太多,即使遇到问题了自行谷歌也会解决的,但是作为前端程序员,服务器端部署就会遇到一些问题了,参考我写的文章服务器如何安装node http://www.cnblogs.com/caichunbao/p/6744909.html 在本地启动node项目可以直接使用node命令运行一个文件,就可以看到效果,但是在服务器上这样做,你会发现关掉shell之后,你的这个node服务就关闭了,所以在服务器环境下需要换一个思路,我看了一些网上的解释,最终找到了

WebStorm 部署node 项目

第一步 windows安装node 安装文件 http://download.csdn.net/detail/wangzhiqiang123456/8628751 安装后在dos 命令下查看是否安装成功 打开cmd 以管理员身份运行 键入指令: node 执行 接着 键入指令: console.log("1"); 执行 正常输出标识安装正常 cmd 中执行node 指令为 node D:\min\sj.js //node js路径 1.使用WebStorm 打开 WebStorm,新建

部署Django项目Nginx + uwsgi

记录一下使用Nginx + uwsgi部署Django项目 关于域名和端口 在这个教程中,我们将假设你的域名为 example.com .用你自己的FQDN或者IP地址来代替. 从头到尾,我们将使用8000端口作为web服务器的公开端口,就像Django runserver默认的那样.当然,你可以使用任何你想要的端口,但是我已经选了这个,因此,它不会与web服务器可能已经选择的任何端口冲突. 基本的uWSGI安装和配置 把uWSGI安装到你的virtualenv中 pip install uws

部署Node.js项目(CentOS)

摘自:https://help.aliyun.com/document_detail/50775.html Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境,用来方便地搭建快速的易于扩展的网络应用.Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高效,非常适合运行在分布式设备的数据密集型的实时应用.Node.js 的包管理器 npm,是全球最大的开源库生态系统.典型的应用场景包括: 实时应用:如在线聊天,实时通知推送等等(如socke

生产环境部署node记录(二):pm2和nginx

安装完node和npm ,接下来安装pm2 首先简单介绍下pm2,官网的介绍是: pm2 是一个带有负载均衡功能的Node应用的进程管理器.当你要把你的独立代码利用全部的服务器上的所有CPU,并保证进程永远都活着,0秒的重载, PM2是完美的.它非常适合IaaS结构,但不要把它用于PaaS方案(随后将开发Paas的解决方案). 备注:SaaS.PaaS和IaaS是云服务模式.        SaaS 软件即服务,例如Google的 Gmail 邮箱服务.面向应用型用户.        PaaS

将node项目部署到linux系统上

1.准备工作,电脑上需要有xshell(远程连接服务器).xftp(上传文件到服务器上).下载node安装包(linux版) 2.使用xshell连接服务器后,进入根目录cd /   创建文件夹名mkdir opt  进入opt目录中 cd /opt 3.ctrl + alt + f 打开xftp ,左侧是Windows目录 右侧界面linux目录把刚才的node安装包拖入到opt项目中,本地应用项目也一起拖放到这个目录中 4.解压缩node安装包 tar -xzvf node-v7.5.0-l

vue+express+mysql项目总结(node项目部署阿里云通用)

原文发布于我的个人博客上:原文点这里 ??前面经历千辛万苦,终于把博客的所有东西都准备好了,现在就只等部署了.下面我介绍下我的部署过程: 一.购买服务器和域名 ??如果需要域名(不用域名通过ip也可以访问,虽然不方便,但可以节约一年几十块钱的域名费用,哈哈),建议提前购买,因为域名备案的时间比较长. ??我是在阿里云上购买的服务器和域名,服务器是在阿里云手机端上购买的活动产品,半年¥9.9(半年后续费好像就是原价了!!!下面是购买的截图),域名是¥55一年(.com比较贵),点这里去挑选合适你的

Jenkins部署Python项目实战

一.背景 我们工作中常用Jenkins部署Java代码,因其灵活的插件特性,例如jdk,maven,ant等使得java项目编译后上线部署一气呵成,同样对于脚本语言类型如Python上线部署,利用Jenkins强大的插件功能,轻松实现CI/CD,但如果部署多项目到同一台服务器涉及环境一致性问题,对此可以利用容器技术Docker解决,也可以利用Python虚拟环境例如virutalenv或conda等优秀等工具解决,在此由于后期根据requirements来安装依赖包比较慢,且后期需要将Pytho

如何使用百度bae部署web项目

百度bae提供了支持各种开发环境的的应用引擎,包括node.js.php.java等,而且还免费提供了一定容量的mysql.mongodb.redis等数据库,所以,可以把它当作一个云服务器来使用.因为价格不贵,最低配置的才2毛钱一天(虽然说是最低配置,其实如果只是用来学习的话已经足够的了!).好吧,说了一大坨的废话,该进入正题了,说说如何使用bae吧,希望对那些没有用过bae引擎的又想找个廉价的服务器的盆友们有所帮助哈! 首先,打开bae的首页:https://bce.baidu.com/pr