FG
📡 Networking

routing broken with express server

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score52%
52%

Problem

I have following setup $ node -v v0.6.11 $ npm list blah/blah/blee/nodex ├─┬ express@2.5.8 │ ├─┬ connect@1.8.5 │ │ └── formidable@1.0.9 │ ├── mime@1.2.4 │ ├── mkdirp@0.3.0 │ └── qs@0.4.2 ├─┬ http-proxy@0.8.0 │ ├── colors@0.6.0-1 │ ├─┬ optimist@0.2.8 │ │ └── wordwrap@0.0.2 │ └── pkginfo@0.2.3 └─┬ socket.io@0.9.1-1 ├── policyfile@0.0.4 ├── redis@0.6.7 └─┬ socket.io-client@0.9.1-1 ├─┬ active-x-obfuscator@0.0.1 │ └── zeparser@0.0.5 ├── uglify-js@1.2.5 ├─┬ ws@0.4.8 │ ├── commander@0.5.0 │ └── options@0.0.2 └── xmlhttprequest@1.2.2 Issue: I have this simple express server [code block] And following simple proxy.js [code block] Just doesnt works! $ curl -XGET http://localhost:8080/ -v ... ok! ... $ curl -XGET http://localhost:8081/ -v blah blah... NOT FOUND ..blah bleh ..

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix Routing Issue in Express Server with Proxy Setup

Medium Risk

The Express server is not properly configured to handle requests routed through the proxy. The proxy is likely not forwarding requests correctly to the Express server, which results in a 'NOT FOUND' error when accessing the proxy URL.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Update Express and Node.js

    Upgrade Node.js to a more recent version (at least v10.x) and update Express to a stable version (at least v4.x) to ensure compatibility and access to improved routing features.

    bash
    nvm install 14 && nvm use 14 && npm install express@latest
  2. 2

    Check Proxy Configuration

    Ensure that the proxy is correctly configured to forward requests to the Express server. Verify that the target URL in the proxy setup matches the Express server's listening port.

    javascript
    const proxy = require('http-proxy').createProxyServer();
    
    proxy.on('error', function(err, req, res) {
      res.writeHead(500, {
        'Content-Type': 'text/plain'
      });
      res.end('Something went wrong.');
    });
    
    app.use('/api', function(req, res) {
      proxy.web(req, res, { target: 'http://localhost:8080' });
    });
  3. 3

    Define Express Routes

    Make sure that the Express server has defined routes that match the requests being sent through the proxy. Add a simple route to handle GET requests at the root path.

    javascript
    app.get('/', function(req, res) {
      res.send('ok!');
    });
  4. 4

    Test the Proxy Setup

    After making the changes, restart both the Express server and the proxy server. Use curl to test the proxy endpoint and ensure it correctly forwards requests to the Express server.

    bash
    curl -XGET http://localhost:8081/api/ -v

Validation

Confirm the fix by running the curl command to the proxy endpoint. You should receive a response from the Express server indicating 'ok!'. If you still receive a 'NOT FOUND' error, check the console for any error messages and verify the configurations.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

proxyhttpnode.js