基于Node.js + Express的应用程序不能与iisnode一起运行

我正在做一些node.js + express和iisnode的实验。 我有以下非常简单的应用程序,位于C:\tsapp-deploy\tsappsvr\TestExpress\0.0.0

app.js:

 var express = require('express'); var app = express(); app.use(express.static(__dirname + "/public")); var port = process.env.PORT || 2709; app.listen(port, function() { console.log('Listening on port ' + port); }); 

的package.json:

 { "name": "TestExpress", "version": "0.0.0", "private": true, "dependencies": { "express": "3.x" } } 

web.config中:

 <configuration> <system.webServer> <handlers> <add name="iisnode" path="app.js" verb="*" modules="iisnode" /> </handlers> </system.webServer> </configuration> 

公共/ index.html的:

 <!doctype html> <html> <head> <script language="javascript" type="text/javascript" src="js/jquery.js"></script> <script language="javascript" type="text/javascript"> $(document).ready(function() { console.log("READY!"); $("#hello").html("Hello, World!"); }); </script> </head> <body> <h1 id="hello" style="text-align:center;"></h1> </body> </html> 

我的configuration是:Windows 8.1,IIS 8; iisnode版本0.2.11,节点版本v0.10.28。

从命令行( C:\tsapp-deploy\tsappsvr\TestExpress\0.0.0>node app.js )启动时,应用程序按预期运行:在浏览器中,我转到http://localhost:2709/并查看“你好,世界!”。

我的IIS当前正在运行其他基于node.js的应用程序不使用来自类似位置(即从C:\tsapp-deploy\tsappsvr\appname\xyz\index.js )的expression式,所以我认为它应该被正确configuration,但是当我尝试从浏览器运行这个应用程序,键入http://localhost/tsappsvr/TestExpress/0.0.0/app.js我得到一个404找不到(在IE中)或“无法GET /tsappsvr/TestExpress/0.0。 Chrome和Firefox中的“0 / app.js”。

我想这个问题可能在我的web.config中,但我不知道如何改变它来让我的应用程序工作。 我已经尝试了一些类似的问题,其他答案build议web.config的一些变化,但没有成功。 有什么build议么?

提前致谢。

我想我已经find了解决我的问题:

  • 首先我安装了url-rewrite扩展,再次感谢David。
  • 其次我改变了我的web.config如下:

web.config中:

 <configuration> <system.webServer> <handlers> <add name="iisnode" path="app.js" verb="*" modules="iisnode" /> </handlers> <rewrite> <rules> <rule name="myapp"> <match url="/*" /> <action type="Rewrite" url="app.js" /> </rule> </rules> </rewrite> </system.webServer> <appSettings> <add key="deployPath" value="/tsappsvr/TestExpress/0.0.0" /> </appSettings> </configuration> 

请注意appSettings部分中的deployPath键,其值设置为应用程序的虚拟path(在IIS中设置)。 在我的情况下是/tsappsvr/TestExpress/0.0.0

  • 最后,我的app.js现在如下所示:

app.js:

 // Preamble, so to say var express = require('express'); var http = require('http'); var app = express(); // Variable deployPath is set in web.config and must match // the path of app.js in virtual directory. // If app.js is run from command line: // "C:/tssapp-deploy/tsappsvr/TestExpress/0.0.0> node app.js" // deployPath is set to empty string. var deployPath = process.env.deployPath || ""; // Static content server app.use(deployPath + "/pages", express.static(__dirname + '/public')); // REST API handler (placeholder) app.all(deployPath + "/api", function(req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end("<h2>REST API Handler found.</h2>"); }); // Default handler app.get(deployPath + "/", function(req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end("<h2>Default Handler found.</h2>"); }); // Not Found handler app.use(function(req, res, next){ res.writeHead(404, {'Content-Type': 'text/html'}); res.write("<h2>The requested resource is not available.</h2>"); res.write("Request received on port " + process.env.PORT + "<br>"); res.write("Request URL: " + req.url + "<br>"); res.write("Deploy Path: " + deployPath + "<br>"); res.end('[iisnode version is ' + process.env.IISNODE_VERSION + ', node version is ' + process.version + ']'); }); // Server creation var server = http.createServer(app); var port = process.env.PORT || 2709; server.listen(port); 

variablesdeployPath用作所有处理程序的前缀(“未find”处理程序除外)。 当从iisnode启动app.js时,deployPath是process.env一部分,而如果从命令行启动app.js,则不确定(例如C:/tsapp-deploy/tsappsvr/TestExpress/0.0.0>node app.js )。 这确保了app.js在这两种情况下都能正常工作。

现在,在浏览器中inputhttp://localhost/tsappsvr/TestExpress/0.0.0/http://localhost/tsappsvr/TestExpress/0.0.0/默认处理程序; 附加/pages到前面的URL我有权访问静态内容,而追加/api我激发REST API处理程序。 http://localhost:2709/作为基本URL也是如此。

Eric,你有没有安装url-rewrite扩展 ?

这是我在我的web.config中的configuration:

 <configuration> <system.webServer> <handlers> <add name="iisnode" path="app.js" verb="*" modules="iisnode" /> </handlers> <rewrite> <rules> <rule name="myapp"> <match url="/*" /> <action type="Rewrite" url="app.js" /> </rule> </rules> </rewrite> <system.webServer> <configuration>