Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

335
Visualizações
Regex not matching in Fastify route

I decided to port my code from Express to Fastify. So this is a big headache when we haven't set proper testing.

Anyway, the route is declared as:

fastify.get(/^\/(donations|skills|blogs)/, async function (req, reply) {

It was working in Express but in Fastify it is returning 404. I'm sure it has to do with regex itself as other routes inside the same plugin/file/ are working properly.

It is supposed to match /listings/donations or listings/skills... knowing that /listings is the prefix when attaching the whole routing plugin to the app.

Regex working

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To reply to your answer, you can't provide a RegExp object to Fastify. You need to set a path-parameter within a RegExp:

const fastify = require('fastify')({ logger: true })

const handler = async (request, reply) => {
  return { hello: request.params.foo }
}

fastify.get('/listings/:foo(^(donations|skills|blogs)$)', handler)
fastify.listen(8080)

(I think you should get an error for your setup so I opened an issue)

As a suggestion you should not do it: you will hit a performance drop cause of this regexp. I would suggest you write it like so:

fastify.get('/listings/donations)', handler)
fastify.get('/listings/skills)', handler)
fastify.get('/listings/blogs', handler)

Let the router does its job.

Here is the performance comparison:

With RegExp

┌───────────┬─────────┬─────────┬─────────┬─────────┬──────────┬─────────┬─────────┐
│ Stat      │ 1%      │ 2.5%    │ 50%     │ 97.5%   │ Avg      │ Stdev   │ Min     │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼─────────┼─────────┤
│ Req/Sec   │ 68863   │ 68863   │ 75327   │ 75839   │ 73678.55 │ 2791.45 │ 68860   │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼─────────┼─────────┤
│ Bytes/Sec │ 12.5 MB │ 12.5 MB │ 13.7 MB │ 13.8 MB │ 13.4 MB  │ 507 kB  │ 12.5 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴──────────┴─────────┴─────────┘

Without RegExp

┌───────────┬─────────┬─────────┬─────────┬─────────┬──────────┬─────────┬─────────┐
│ Stat      │ 1%      │ 2.5%    │ 50%     │ 97.5%   │ Avg      │ Stdev   │ Min     │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼─────────┼─────────┤
│ Req/Sec   │ 86015   │ 86015   │ 95423   │ 95551   │ 94350.55 │ 2681.36 │ 85954   │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼─────────┼─────────┤
│ Bytes/Sec │ 15.6 MB │ 15.6 MB │ 17.4 MB │ 17.4 MB │ 17.2 MB  │ 491 kB  │ 15.6 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴──────────┴─────────┴─────────┘
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda