在iis上使用HttpPlatformHandler运行node.js应用程序

我试图在iis上运行“Hello World”样本node.js服务器:

var http = require('http'); var server = http.createServer(function(req, res) { res.writeHead(200, {"Content-Type":"text/plain"}); res.end("Hello World\n"); }) server.listen(3000); console.log('server is running'); 

使用这个web.config:

 <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <handlers> <add name="httpplatformhandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script" /> </handlers> <httpPlatform stdoutLogEnabled="true" stdoutLogFile="node.log" startupTimeLimit="20" processPath="C:\Program Files\nodejs\node.exe C:\Users\username\Tests\nodeHandlerTest\app.js"> <environmentVariables> </environmentVariables> </httpPlatform> </system.webServer> </configuration> 

它不工作! 任何想法是什么问题与我的web.config文件?

  • 对于端口,模块将使用%HTTP_PLATFORM_PORT%访问的随机端口启动进程。 所以你应该把它映射到一个Node envvariables,否则当IIS尝试在回收时启动多个Node进程时,会得到地址使用错误。
  • 应该在arguments属性中指定app.js的path。

所以你的app.js应该是这样的:

 var http = require('http'); var server = http.createServer(function(req, res) { res.writeHead(200, {"Content-Type":"text/plain"}); res.end("Hello World\n"); }) var port = process.env.PORT || 3000; server.listen(port); console.log('server is running on port: ' + port); 

web.config

 <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <handlers> <add name="httpplatformhandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script" /> </handlers> <httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\node.log" startupTimeLimit="20" processPath="C:\Program Files\nodejs\node.exe" arguments=".\app.js"> <environmentVariables> <environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" /> <environmentVariable name="NODE_ENV" value="Production" /> </environmentVariables> </httpPlatform> </system.webServer> </configuration> 

请注意,如果path以“。”开头,则支持v1.2相对path。 该path被认为是相对于站点根目录的,因此.\到log和app.js文件。 更多信息在这里 。

web.config文件的示例,对命令文件使用参数标记:

 <configuration> <system.webServer> <handlers> <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script" /> </handlers> <httpPlatform forwardWindowsAuthToken="true" stdoutLogEnabled="false" processesPerApplication="1" startupTimeLimit="120" processPath="C:\ruby2_jruby\bin\jruby.exe" arguments=" -J-XX:+TieredCompilation -J-XX:TieredStopAtLevel=1 -J-noverify -J-Xmn256m -J-Xms512m -J-Xmx512m -J-server -J-XX:+UseParallelGC -J-XX:ParallelGCThreads=4 -J-Djruby.jit.threshold=10 -J-Djruby.jit.max=16384 -J-XX:CompileThreshold=10 -J-XX:ReservedCodeCacheSize=128M -J-d64 -J-Dfile.encoding=UTF8 -S puma --env production --dir C:\inetpub\wwwroot\jruby\redmine -p %HTTP_PLATFORM_PORT% -t 10:20 "> <environmentVariables> <environmentVariable name="JAVA_HOME" value="C:\Program Files\Java\jre1.8\bin\java.exe" /> <environmentVariable name="RAILS_ENV" value="production" /> <environmentVariable name="RACK_ENV" value="production" /> </environmentVariables> </httpPlatform> <httpErrors errorMode="Detailed" /> </system.webServer> <system.web> <identity impersonate="true" password="***" userName="***" /> </system.web> </configuration>