简体   繁体   中英

Apache / NodeJS / Proxy with support for subdirectories

My Apache Config:

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

My 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');
});

This currently works when connecting to mydomain.com : it will successfully return index.html . My problem occurs when I try to connect to mydomain.com/foo . Ideally this would simply go to http://localhost:3000/foo but instead it says "Cannot GET /foo" .

How would I redirect mydomain.com/* to the respective path in my NodeJS project directory without tons of:

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');
});

?

You should use the static files serving module with express :

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

In your case, something like that :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM