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

334
Views
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 answers
Answer question

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 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!