How to set up Apache ProxyPass to preserve Express routes
Asked Answered
A

2

8

In my Apache config im forwarding all traffic on /node to port 3000, where the Express server is listening.

<IfModule mod_proxy.c>

  ProxyRequests Off

  ProxyPass /node http://localhost:3000/

</IfModule>

The Express app looks like this:

var express = require('express');

var app = express();
var router = express.Router();

router.route('/route/:id').get(function (req, res) {

    res.json({ description: 'Welcome to a route with an ID' }); 
});

router.route('/route').get(function (req, res) {

        res.json({ description: 'Welcome to the normal route' }); 
});

router.route('/').get(function (req, res) {

    res.json({ data: 'Welcome to the app' }); 
});

app.use('/', router);

app.listen(3000);

When I direct my browser to http://myserver.com/node I get the response { data: 'Welcome to the app' }, which is fine. Though, when I try to go http://myserver.com/node/route or http://myserver.com/node/1210 I get an error Cannot GET //route.

Any ideas how I can update my Apache config to preserve the Express routes?

I'm running Apache 2.4.6 on CentOS.

Afterclap answered 15/10, 2015 at 15:32 Comment(2)
It is only a hint: it's used nginx proxy node, not thought about using nginx to apache inves?Sprang
Thanks! But unfortunately using nginx is not an option.Afterclap
R
11

You have an extra / at the end of your host. Try changing it to:

ProxyPass /node http://localhost:3000
Rena answered 15/10, 2015 at 16:28 Comment(1)
Thank you very much. That did the trick. Now I will go facepalm myself.Afterclap
A
0

I had the same problem after settings virtual host on apache 2 server with my nodejs backend system here is how i fixed it:

    ProxyRequests On
    ProxyVia Full
    ProxyPreserveHost on
    ProxyPass /api http://jwappengine.com:7000
    ProxyPass /secure http://jwappengine.com:1442
    ProxyPass /host https://jwappengine.com:1400
    ProxyPass /connect/v1 https://jwappengine.com:1400
    ProxyPass /payment https://jwappengine.com:3000
Achaemenid answered 3/9, 2021 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.