Nest.js升级指南-从v7到v8
升级指南 #
这篇文章提供了一些关于从 Nest v7 到 v8 的升级指南,学习更多关于我们在 v8 添加的新特性,请访问 链接.
HTTP 模块 #
从 @nestjs/common
包导出的 HttpModule
模块已经被废弃,并且我们将在下个主版本中将其删除。
作为替换,请使用 @nestjs/axios
包(它们的API接口没有任何区别)。
Terminus #
HttpHealthIndicator
HttpHealthIndicator
requires @nestjs/axios
to be installed as well as HttpModule
to be imported. Otherwise, there are no API differences.
TerminusModule.forRoot{Async}
The deprecated TerminusModule.forRootAsync
has been removed. To migrate, check out the @nestjs/terminus
v7.x.x upgrade guide
gRPC strategy #
The original Node gRPC library (grpc
) has been deprecated and will no longer receive feature updates.
With Nest v8, you should use the @grpc/grpc-js
library instead.
NATS strategy #
NATS has released a new major version (2.0) which has many changes and it is not API compatible with nats@1.x.x
.
If you interact with a Nest microservice (that uses NATS as a transfer layer), from a service written in a different framework, please, see their migration document to learn what's changed in v2. Otherwise, you should not see any major differences when communicating between Nest microservices.
To upgrade, make sure to install the latest version of the nats
package (npm i nats@latest
). Also, update your NATS configuration. Example:
// Before
const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
transport: Transport.NATS,
options: {
url: 'nats://localhost:4222',
},
});
// Now
const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
transport: Transport.NATS,
options: {
servers: ['nats://localhost:4222'],
},
});
@All()
decorator #
Routes annotated with the @All()
decorator will now map to the router.all()
method instead of the router.use()
.
Async listen/start methods #
listenAsync()
and startAllMicroservicesAsync()
methods have been deprecated.
Instead, simply use the listen()
and startAllMicroservices()
methods (they are async
either way).
Socket.io #
The @nestjs/platform-socket.io
package was upgraded to use the socket.io@4.x.x
version (Nest v7 was based on the socket.io
v2).
To learn more, check out these articles: Socket.io 3 Release and Socket.io 4 Release.
Logger breaking changes #
For better extensibility, we separated out the Logger
and ConsoleLogger
classes (PR, learn more in the Logging chapter). If your application uses a custom logger class that extends the built-in Logger
, you should update it to extend the ConsoleLogger
now.
Before:
export class MyLogger extends Logger {}
Now:
export class MyLogger extends ConsoleLogger {}
@nestjs/config
package #
There was a minor breaking change in the registerAs
function (typings), you can see what has changed in this PR.
@nestjs/graphql
package #
There might be some small differences in how your auto-generated schema file looks like (changed types order). Also, if you use the schema-first approach, the automatically generated type definitions will change as there was a new Nullable<T>
type introduced in the latest release.
Also, all HttpException
errors thrown from your resolvers will be now automatically mapped to the corresponding ApolloError
instances, unless you set the autoTransformHttpErrors
configuration property (in the options object you pass into the GraphQLModule#forRoot()
method) to false
.
RxJS #
确保将 rxjs
软件包升级到最新版本(v7).
- 下一篇: Nest.js升级指南-从v8到v9