Skip to content

Commit d4cb393

Browse files
docs: update with the latest version of api
1 parent 4a02aa6 commit d4cb393

File tree

1 file changed

+37
-60
lines changed

1 file changed

+37
-60
lines changed

content/microservices/basics.md

Lines changed: 37 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -413,87 +413,64 @@ After 5 seconds, if the microservice isn't responding, it will throw an error.
413413

414414
#### TLS support
415415

416-
Whenever we leave a private network we should encrypt our traffic. Nest supports TLS over TCP with the default TCP client by utilizing Nodes [TLS](https://nodejs.org/api/tls.html) module. In order to use TLS we need to pass `useTls: true` to the options.
416+
WWhen communicating outside of a private network, it’s important to encrypt traffic to ensure security. In NestJS, this can be achieved with TLS over TCP using Node's built-in [TLS](https://nodejs.org/api/tls.html) module. Nest provides built-in support for TLS in its TCP transport, allowing us to encrypt communication between microservices or clients.
417417

418-
For creating a TLS Server we need to create or obtain a private key in PEM format as well as a certificate in PEM format and add it to the servers options:
418+
To enable TLS for a TCP server, you'll need both a private key and a certificate in PEM format. These are added to the server's options by setting the `tlsOptions` and specifying the key and cert files, as shown below:
419419

420420
```typescript
421421
import * as fs from 'fs';
422+
import { NestFactory } from '@nestjs/core';
423+
import { AppModule } from './app.module';
424+
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
422425

423426
async function bootstrap() {
424-
const key = fs.readFileSync(<pathToKeyFile>);
425-
const cert = fs.readFileSync(<pathToCertFile>);
426-
const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule,
427-
{
428-
transport: Transport.TCP,
429-
options: {
430-
useTls: true,
431-
key,
432-
cert
433-
}
434-
});
435-
await app.listen();
436-
}
437-
```
438-
439-
A TCP TLS client can be created using the same approach as the TCP client from above, but we need to set `useTls = true` as well.
440-
The client will fetch the servers certificate by default:
427+
const key = fs.readFileSync('<pathToKeyFile>', 'utf8').toString();
428+
const cert = fs.readFileSync('<pathToCertFile>', 'utf8').toString();
441429

442-
```typescript
443-
@Module({
444-
imports: [
445-
ClientsModule.register([
446-
{ name: 'MATH_SERVICE', transport: Transport.TCP, options: { useTls: true} },
447-
]),
448-
]
449-
...
450-
})
430+
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
431+
AppModule,
432+
{
433+
transport: Transport.TCP,
434+
options: {
435+
tlsOptions: {
436+
key,
437+
cert,
438+
},
439+
},
440+
},
441+
);
451442

443+
await app.listen();
444+
}
445+
bootstrap();
452446
```
453447

454-
If we are using self-signed certificates we need to pass the CA(s) in PEM format to our client as well:
448+
For a client to communicate securely over TLS, we also define the `tlsOptions` object but this time with the CA certificate. This is the certificate of the authority that signed the server's certificate. This ensures that the client trusts the server's certificate and can establish a secure connection.
455449

456450
```typescript
457-
import * as fs from 'fs';
458-
459-
const ca = fs.readFileSync(<pathToCAFile>);
451+
import { Module } from '@nestjs/common';
452+
import { ClientsModule, Transport } from '@nestjs/microservices';
460453

461454
@Module({
462455
imports: [
463456
ClientsModule.register([
464-
{ name: 'MATH_SERVICE',
457+
{
458+
name: 'MATH_SERVICE',
465459
transport: Transport.TCP,
466-
options: { useTls: true, ca}
460+
options: {
461+
tlsOptions: {
462+
ca: [fs.readFileSync('<pathToCaFile>', 'utf-8').toString()],
463+
},
464+
},
467465
},
468466
]),
469-
]
470-
...
467+
],
471468
})
469+
export class AppModule {}
472470
```
473471

474-
We can also pass an array of CAs to the Client:
475-
476-
```typescript
477-
import * as fs from 'fs';
478-
479-
const ca1 = fs.readFileSync(<pathToCAFile1>);
480-
const ca2 = fs.readFileSync(<pathToCAFile2>);
481-
482-
@Module({
483-
imports: [
484-
ClientsModule.register([
485-
{ name: 'MATH_SERVICE',
486-
transport: Transport.TCP,
487-
options: { useTls: true, ca: [ca1, ca2]}
488-
},
489-
]),
490-
]
491-
...
492-
})
493-
```
472+
You can also pass an array of CAs if your setup involves multiple trusted authorities.
494473

495-
Then we can inject the `ClientProxy` as usual using `@Inject('MATH_SERVICE')`
474+
Once everything is set up, you can inject the `ClientProxy` as usual using the `@Inject()` decorator to use the client in your services. This ensures encrypted communication across your NestJS microservices, with Node's `TLS` module handling the encryption details.
496475

497-
For further information refer to Nodes [TLS](https://nodejs.org/api/tls.html) module:
498-
The server uses the `createServer(options)` method.
499-
The client uses the `TLSSocket()` constructor.
476+
For more information, refer to Node’s [TLS documentation](https://nodejs.org/api/tls.html).

0 commit comments

Comments
 (0)