NestJS

1,478次阅读
没有评论

Fastify上传文件

@nestjs/common包中的UploadedFile只能用于express的适配器,而用来fastify的,那么需要从fastify对应的包导出。

Fastify 托管静态文件、图片

app.useStaticAssets({
    root: join(__dirname, '..', 'uploads'),
    prefix: '/public',
  });

注意点:publicpublic/ 都会报错。

模型定义与参数验证合并

  • 参数过滤
    • 分页

In Express.js version 5 and later, req.query is an object that holds the query parameters parsed from the URL’s query string. These query parameters are essentially key-value pairs that appear after a question mark (?) in the URL, separated by ampersands (&) if there are multiple parameters. 

Here’s a breakdown of how req.query works in Express 5:

  • Key-Value Pairs: The req.query object contains properties corresponding to each query parameter, where the property name is the parameter’s key and the value is its associated value from the URL.
  • Example: For a URL like https://example.com/user?name=Theodore&isAuthor=truereq.query would be { name: "Theodore", isAuthor: true }.
  • Accessing Values: You can access the value of a specific query parameter using dot notation, e.g., req.query.name would give you "Theodore"

Important considerations for Express v5

  • Immutable Getter: In Express v5, req.query is now a getter that reflects the contents of req.url. This means you cannot directly modify req.query like you might have in earlier versions. If you need to alter the query for transparent processing, you’ll need to modify req.url itself.
  • Validation and Transformation: If you need to validate or transform query parameters, you can read their values from req.query and then assign them to a different property on the req object (e.g., req.validatedQuery). 

In essencereq.query remains the primary way to access URL query parameters in Express 5, but its behavior regarding modification has changed. You can still easily read and utilize the parsed query string data, but direct manipulation of req.query is no longer supported. 

正文完
 0
wujingquan
版权声明:本站原创文章,由 wujingquan 于2023-12-15发表,共计1728字。
转载说明:Unless otherwise specified, all articles are published by cc-4.0 protocol. Please indicate the source of reprint.
评论(没有评论)