网站搜索

node.js - 如何解决 nestjs 和 fastify 中的“TypeError: req.pipe 不是一个函数”

我尝试用 NestJS/Fastify 和 TypeScript 上传文件

这是 main.ts

async function bootstrap() { //file upload with fastify const fastifyAdapter = new FastifyAdapter(); fastifyAdapter.register(fmp, { limits: { fieldNameSize: 100, // Max

阅读更多 →

node.js - 如何解决以下 TypeScript 错误:“找不到命名空间 'fastify'”?

我最近决定升级我 fastify-server 模板上的所有包。

我的 package.json 看起来是这样的:

{ "name": "backend-template", "version": "1.0.0", "description": "", "author": "", "

阅读更多 →

Visual Studio Code - 如何设置 TypeScript、VSCode、Yarn2、Fastify 项目

我知道我以前解决过这个问题,但也许自那以后情况已经改变。我正在尝试建立一个主要使用 Fastify 的新项目,在 VSCode 中使用 TypeScript 编码,并使用 Yarn2 管理依赖项。

VSCode 告诉我它找不到 fastify。尽管我已经添加了 fastify 和 @types/node,它仍然找不到 Node 类型。我尝试了各种组合和以下步骤,确保我使用的是 VSCode 工作区版本的 TypeScript,修改了 tsconfig.json 以使用 node moduleResolution 等。以前能用的东西现在都不管用了,但我没找到任何其他当前的帖子或文档能解决这个问题。

我漏掉了什么?

nvm install node ## Install latest NodeJS node --version > .nvmrc ## Set the node version to use yarn init -y ## Projec

阅读更多 →

json - fastify 回复:将 null 视为 undefined

在使用 Java 时,注解 @JsonInclude(Include.NON_NULL) 会导致空值在最终的 JSON 输出中被省略。例如,对象 {a: 5, b: null} 会被序列化为 {a: 5}。

Fastify/Typescript 对未定义的值已经有类似的表现。例如,对象 {a: 5, b: undefined} 会被序列化为 {a: 5}。

但是有没有办法让 Fastify/Typescript 对 null 值的处理方式类似?目前,对象 {a: 5, b: null} 正在序列化为 {a: 5, b: null},而不是 {a: 5}。

到目前为止,我的解决方法是单独检查每个字段并将 null 替换为 undefined,但这既繁琐又容易出错。希望能找到一个更简洁的解决方案!

阅读更多 →

node.js - tsup --watch 在 macOS 15.0.1 上的 Fastify 应用中使用 server.listen 时重建/重启

我正在 macOS 15.0.1 上开发一个 Fastify 应用程序,每当我包含 server.listen 时,重建和重启应用程序都会遇到问题。我尝试过使用 tsup 进行监视和重建,但一旦我添加 server.listen,监视模式就停止检测文件更改。

这是我的设置分解:

import buildServer from './server'; async function main(): Promise<void> { const server = await buildServer(); server.listen({ port: 4000, host: '0.0.0.0' }); } void main();

server.ts

import type { FastifyInstance } from 'fastify'; import fastifyHelmet from '@fastify/helmet'; import Fastify fr

阅读更多 →

TypeScript - 在使用 NestJS 和 Fastify 时 Angular 8 的静态内容路由问题

在过去的几天里,我一直在寻找一种解决使用 NestJS 和 Fastify 提供静态内容时的路由问题的方法。具体来说,我正尝试使用由 NestJS 托管、底层使用 Fastify 的 Angular 8。我一直在遵循教程中给出的示例:https://www.djamware.com/post/5d2898430707cc5968d9d57f/build-a-web-app-using-nestjs-fastify-mongodb-and-angular-8

问题在于,如果你尝试直接导航到一个特定的 URL,例如 ht

阅读更多 →

node.js - Fastify Swagger,为 Fastify 路由添加标签和描述

我正在使用 fastify 3.28.0(加上 fastify-swagger 插件)和 typescript 4.6.2,我想在每个路由中添加标签、描述和摘要。

来自文档:fastify swagger 可以为每个路由添加描述、标签和摘要。

但我无法做到这一点,因为模式的定义没有提供它们:

export interface FastifySchema { body?: unknown; querystring?: unknown; params?: unknown; headers?: unknown; response?: unknown; }

所以在这种情况下,我不知道如何在 Fastify 路由中定义它们:

const userRoutes: FastifyPluginCallback = (fastify, _opts, done) => { const findRoute: RouteOptions = { method

阅读更多 →

typescript - 使用 Fastify 的 Busboy 报错 Unsupported Media Type: multipart/form-data; boundary

我正在尝试在 Fastify 中使用 BusBoy 上传文件

但是我收到了这个错误

Unsupported Media Type: multipart/form-data; boundary=--------------------------964469693542161018443025 reqId: "req-1" res: { "statusCode": 415 } err: {

阅读更多 →

node.js - 使用 fastify-autoload 时绝对导入不起作用

我正在使用 TypeScript 构建 Fastify 应用程序,并且在使用绝对导入时遇到了问题。

{ "extends": "fastify-tsconfig", "compilerOptions": { "outDir": "dist", "sourceMap": true, "experimentalDecorators": true, "strictPropertyInitializati

阅读更多 →

node.js - Fastify json-schema-to-ts 类型提供者不起作用

我正在尝试在 Fastify 中从 JSON 模式实现类型推断。

文档中的这个示例对我不起作用,因为 request.query 仍然是 unknown,有时甚至是 {}。

这是 stackblitz 上文档的直接复制粘贴。如你所见,TypeScript 编译失败。

有什么主意吗?

答案#1

Querystring 类型未定义,因此出现错误。要定义它,可以按如下方式操作:

阅读更多 →