I have a node.js express application running on machine1 on port 3000. I can successfully access my application via machine1:3000
I have HAProxy running at machine2. I updated haproxy.cfg as follows
frontend main *:80
acl url_mynodeapp path_beg -i /mynodeapp
use_backend mynodeapp if url_mynodeapp
backend mynodeapp
mode http
reqrep ^([^\ ]*\ /)mynodeapp[/]?(.*) \1\2
balance roundrobin
server machine1 1.1.1.1:3000
I can now hit my application at machine2/mynodeapp. However all relative links are now broken, including css and javascript (since they point to machine2 instead of machine2/mynodeapp).
How do you deploy an express application behind a proxy? How do you deploy an express application with a subfolder? I am coming from the java world were all web applications are name spaced inside the servlet container, thus giving each application its own subfolder.
Thanks,
Nathan
/mynodeapp
isn't a subdomain. It's a subfolder. Subdomain would be mynodeapp.machine2
Subdomain hosting works fine with express, since that is not express's job, rather the front-end's.
As long as your front-end resolves consistently, (which it would with subdomains, not subfolders), express will receive expected urls from HAproxy
The only solution I could find was manually setting up the node application to run from a subfolder.
I used the solutions provided from How to handle relative paths in node.js / express? and call functions from with ejs templates on node
var subfolder = '/mynodeapp';
app.set('view engine', 'ejs');
app.use(subfolder, app.router);
app.use(subfolder, express.static(__dirname + '/public'));
app.locals.createLink = function(uri) {
return subfolder + uri;
}
Then in ejs views, create links via function call createLink.
<script src="<%= createLink('/javascript/myjavascript.js') %>">
<a href="<%= createLink('/') %>">Home</a>
Now I have a node.js express application running on at machine1:3000/mynodeapp.
I can then easily route thru a proxy and access my application at machine2/mynodeapp. Note: in the proxy config, just remove the regrep line.
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.