Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
Fastify-http-proxy unable to get dynamic routes

I have a fastify server that's pointing to two "backend" fastify servers and I'd like to generate a random number that is then used to decide which backend to route to.

Currently it only works the first time, and I'm unable to override the upstream afterwards.

server.register(require('fastify-http-proxy'), {
  upstream: '',
  replyOptions: { 
    getUpstream: (originalReq, base) => {
      if (Math.random() < 0.5) {
        console.log(`setting base to backend1`)
        return `http://localhost:${backend1Port}`
      } else {
        console.log(`setting base to backend2`)
        return `http://localhost:${backend2Port}`
      }
    },
    disableCache: true,
    cacheURLs: 0,
  },
})

How can I make it so that upon refresh it might change where it routes to?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Figured it out. disableCache needed to be outside of replyOptions.

server.register(require('fastify-http-proxy'), {
  upstream: '',
  replyOptions: { 
    getUpstream: (originalReq, base) => {
      if (Math.random() < 0.5) {
        console.log(`setting base to backend1`)
        return `http://localhost:${backend1Port}`
      } else {
        console.log(`setting base to backend2`)
        return `http://localhost:${backend2Port}`
      }
    },
    disableCache: true,
    cacheURLs: 0,
  },
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!