Reputation: 901
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.
Upvotes: 1
Views: 1256
Reputation: 12900
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:
┌───────────┬─────────┬─────────┬─────────┬─────────┬──────────┬─────────┬─────────┐
│ 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 │
└───────────┴─────────┴─────────┴─────────┴─────────┴──────────┴─────────┴─────────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬──────────┬─────────┬─────────┐
│ 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 │
└───────────┴─────────┴─────────┴─────────┴─────────┴──────────┴─────────┴─────────┘
Upvotes: 1