[英]socket.io routing issue with a node.js express 3 app running in a virtual directory with iisnode
[英]Node.js + Express based app not running with 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>
我的配置是: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的应用程序,而不是使用来自类似位置的express(即来自C:\\tsapp-deploy\\tsappsvr\\appname\\xyz\\index.js
),所以我认为应该正确配置它,但是当我尝试从浏览器运行此应用程序,输入http://localhost/tsappsvr/TestExpress/0.0.0/app.js
我得到404未找到(在IE中)或“无法获取/tsappsvr/TestExpress/0.0。 0 / app.js“在Chrome和Firefox中。
我想问题可能出在我的web.config中,但我无法弄清楚如何更改它以使我的应用程序正常工作。 我已经尝试了对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
键,其值设置为应用程序的虚拟路径(在IIS中设置)。 在我的例子中,它是/tsappsvr/TestExpress/0.0.0
。
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);
变量deployPath
用作所有处理程序的前缀(“未找到”处理程序除外)。 当app.js从iisnode启动时,deployPath它是process.env
一部分,而如果从命令行启动app.js它是未定义的(例如C:/tsapp-deploy/tsappsvr/TestExpress/0.0.0>node app.js
)。 这可以确保app.js在两种情况下都能正常工作。
现在,在浏览器中键入http://localhost/tsappsvr/TestExpress/0.0.0/
,我将触发默认处理程序; 将/pages
附加到以前的URL我可以访问静态内容,而附加/api
REST API处理程序。 http://localhost:2709/
作为基本URL也是如此。
Eric,你安装了url-rewrite扩展吗?
这是我在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>
<configuration>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.