Apache / NodeJS / Proxy,支持子目录

时间:2015-08-19 05:20:49

标签: node.js apache mod-proxy

我的Apache配置:

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyRequests Off
    ServerName mydomain.com
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
</VirtualHost>

我的NodeJS:

var app = require('express')();
var http = require('http').Server(app);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

http.listen(3000, function(){
  console.log('Listening on *:3000');
});

这在连接到mydomain.com时目前有效:它将成功返回index.html。当我尝试连接到mydomain.com/foo时出现问题。理想情况下,这只会转到http://localhost:3000/foo,而是说"Cannot GET /foo"

如何将mydomain.com/*重定向到我的NodeJS项目目录中的相应路径,而不是大量的:

app.get('/foo', function(req, res){
  res.sendFile(__dirname + '/foo');
});

app.get('/bar', function(req, res){
  res.sendFile(__dirname + '/bar');
});

app.get('/foo/bar', function(req, res){
  res.sendFile(__dirname + '/foo/bar');
});

1 个答案:

答案 0 :(得分:0)

您应该使用express:

使用静态文件服务模块

http://expressjs.com/starter/static-files.html

在你的情况下,类似的东西:

app.use(express.static(__dirname, {
  index: 'index.html'
}))