I set the Apache reverse proxy to forward api.mydomain.comto localhost:2000, which works fine.
However, the real problem that arises with me is related to sessions - it seems that req.session is not saved when querying through api.mydomain.com. Sessions will work with a wonderful visit localhost:2000.
I assume this has something to do with the proxy server ...
server.js
var express = require('express'),
app = express();
app.enable('trust proxy');
app.configure(function() {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'supersecret'});
app.use(app.router);
});
app.get('/session', function(req, res, next){
console.log(req.session.username)
});
app.post('/session', function(req, res, next){
req.session.username = 'my username';
});
apache config
<VirtualHost *:80>
ServerName api.mydomain.com
ProxyPass / http://localhost:2000/
ProxyPassReverse / http://localhost:2000/
ProxyPreserveHost On
ProxyPassReverseCookieDomain api.domain.com localhost:2000
ProxyPassReverseCookiePath / /
</VirtualHost>
EDIT
It is noteworthy that for further testing - adding the code below app.use(app.router)to app.configure()....
app.use(function (req, res) {
res.send('<h2>Hello, your session id is ' + req.sessionID + '</h2>');
});
will lead to the following (each line represents a new GET / session request for the application - node also did not reboot).
local: 2000
Hello, your session id is sHkkESxJgzOyDhDwyTjwpNzq
Hello, your session id is sHkkESxJgzOyDhDwyTjwpNzq
Hello, your session id is sHkkESxJgzOyDhDwyTjwpNzq
api.mydomain.com
Hello, your session id is uuo4U5ierZAG8LSH1BdwTlVf
Hello, your session id is 8BxL97Bo35SDt4uliuPgnbia
Hello, your session id is 0xkqZZpzQNvTsQpbJtUlXgkR
Setup Information
NodeJS v0.8.8
Apache v2.4.2
ExpressJS v3.0.0rc4
UPDATE 2
, mydomain.com/api, , , . , , Apache Express!