You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/microservices/basics.md
+37-60Lines changed: 37 additions & 60 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -413,87 +413,64 @@ After 5 seconds, if the microservice isn't responding, it will throw an error.
413
413
414
414
#### TLS support
415
415
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.
417
417
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:
419
419
420
420
```typescript
421
421
import*asfsfrom'fs';
422
+
import { NestFactory } from'@nestjs/core';
423
+
import { AppModule } from'./app.module';
424
+
import { MicroserviceOptions, Transport } from'@nestjs/microservices';
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.
455
449
456
450
```typescript
457
-
import*asfsfrom'fs';
458
-
459
-
const ca =fs.readFileSync(<pathToCAFile>);
451
+
import { Module } from'@nestjs/common';
452
+
import { ClientsModule, Transport } from'@nestjs/microservices';
You can also pass an array of CAs if your setup involves multiple trusted authorities.
494
473
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.
496
475
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