支持子目录的 Apache / NodeJS / Proxy

Apache / NodeJS / Proxy with support for subdirectories

我的 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 时有效:它将成功 return 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');
});

?

您应该将静态文件服务模块与 express 一起使用:

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

在你的情况下,类似的事情:

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