Skip to content

Proxying with nested prefix #8

@yamalight

Description

@yamalight

What are you trying to achieve or the steps to reproduce?

I'm trying to create a proxy on nested prefix, e.g. /api/test.
Here's the simple example that reproduces the behavior:

// index.js
const fastify = require('fastify');
const proxy = require('fastify-http-proxy');

// setup proxy
const proxiedServer = require('./proxiedServer');

// setup app
const app = fastify();

app.get('/', (req, res) => res.send('Hello world!'));

app.register(proxy, {
  upstream: 'http://localhost:8081',
  prefix: '/api/test',
});

app.listen(8080, '0.0.0.0', (err, address) => {
  if (err) {
    console.error(err);
    return;
  }
  console.info(`Listening at ${address}`);
});
// proxiedServer.js
const fastify = require('fastify');
const app = fastify();

app.get('/', (req, res) => res.send('Hello proxied GET world!'));
app.post('/', (req, res) => res.send('Hello proxied POST world!'));

app.listen(8081, (err, address) => {
  if (err) {
    console.error(err);
    return;
  }
  console.info(`Listening at ${address}`);
});

Online test version can be found here on glitch

What was the result you received?

Trying to GET or POST to /api/test returns 404.

What did you expect?

Get correct proxied responses.

Context

  • node version: 8, 10
  • fastify version: 1.7.0
  • fastify-http-proxy version: 0.4.0
  • os: Linux, Mac
  • any other relevant information:

If you change /api/test to /test - it starts working fine.

I've also tried using plugin approach and wrapping proxy in the following block:

app.register((inst, opts, next) => {
  inst.register(proxy, {
    upstream: 'http://localhost:8081',
    prefix: '/test',
  });
  next();
}, {prefix: '/api'});

This still results in 404.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions