简体   繁体   English

Fastify 路由中的正则表达式不匹配

[英]Regex not matching in Fastify route

I decided to port my code from Express to Fastify.我决定将我的代码从 Express 移植到 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.它在 Express 中工作,但在 Fastify 中返回 404。我确信它与正则表达式本身有关,因为同一插件/文件/中的其他路由工作正常。

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.它应该匹配/listings/donationslistings/skills ...知道/listings是将整个路由插件附加到应用程序时的前缀。

Regex working正则表达式工作

To reply to your answer, you can't provide a RegExp object to Fastify.要回复您的答案,您不能向 Fastify 提供 RegExp object。 You need to set a path-parameter within a RegExp:您需要在 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 │
└───────────┴─────────┴─────────┴─────────┴─────────┴──────────┴─────────┴─────────┘

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM