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
There won't always be a time where you're sending back JSON or string responses. There may be times where you'd like to send back a file from the server to the client. In an express application, you may use something like
to manage sending the file back. This is still doable in Nest, by injecting `@Res()` in the controller, but in doing so you end up losing access to your post-controller interceptor logic. To handle this, you can return a `StreamableFile` instance and under the hood Nest will take care of piping the response.
12
+
13
+
#### Streamable File
14
+
15
+
A `StreamableFile` is as class that holds onto the stream that is to be returned. To create a new `StreamableFile`, you can pass either a `Buffer` or a `Stream` to the `StreamableFile` constructor.
16
+
17
+
> info **hint** The `StreamableFile` class can be imported from `@nestjs/common`.
18
+
19
+
#### Cross Platform Support
20
+
21
+
Fastify, by default, can support sending files without needing to `stream.pipe(res)`, so you don't need to use the `StreamableFile` class. However, Nest supports the use of `StreamableFile` in both platform types, so if you end up switching between Express and Fastify there's no need to worry about compatibility between the two engines.
This is of course a simple example of returning the `package.json` as a file instead of a JSON, but the idea extends out naturally to images, documents, and any other file type.
<p>When you run <code>nest new</code>, or clone the <arel='nofollow' target='_blank' href="https://github.com/nestjs/typescript-starter">typescript starter</a>, Nest populates the new project's <code>package.json</code> scripts with commands like <code>build</code> and <code>start</code>. It also installs the underlying compiler tools (such as <code>typescript</code>) as <strong>dev dependencies</strong>.</p>
34
34
<p>You run the build and execute scripts with commands like:</p>
35
35
<pre><codeclass="language-bash">
36
-
$ npm run build</code></pre>
36
+
$ npm run build
37
+
</code></pre>
37
38
<p>and</p>
38
39
<pre><codeclass="language-bash">
39
-
$ npm run start</code></pre>
40
+
$ npm run start
41
+
</code></pre>
40
42
<p>These commands use npm's script running capabilities to execute <code>nest build</code> or <code>nest start</code> using the <strong>locally installed</strong><code>nest</code> binary. By using these built-in package scripts, you have full dependency management over the Nest CLI commands*. This means that, by following this <strong>recommended</strong> usage, all members of your organization can be assured of running the same version of the commands.</p>
41
43
<p>*This applies to the <code>build</code> and <code>start</code> commands. The <code>nest new</code> and <code>nest generate</code> commands aren't part of the build/execute pipeline, so they operate in a different context, and do not come with built-in <code>package.json</code> scripts.</p>
42
44
<p>For most developers/teams, it is recommended to utilize the package scripts for building and executing their Nest projects. You can fully customize the behavior of these scripts via their options (<code>--path</code>, <code>--webpack</code>, <code>--webpackPath</code>) and/or customize the <code>tsc</code> or webpack compiler options files (e.g., <code>tsconfig.json</code>) as needed. You are also free to run a completely custom build process to compile the TypeScript (or even to execute TypeScript directly with <code>ts-node</code>).</p>
Copy file name to clipboardExpand all lines: src/app/homepage/pages/microservices/custom-transport/custom-transport.component.html
+30-15Lines changed: 30 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,8 @@ <h4 appAnchor id="creating-a-strategy"><span>Creating a strategy</span></h4>
39
39
* This method is triggered on application shutdown.
40
40
*/
41
41
close() {}
42
-
}</code></pre>
42
+
}
43
+
</code></pre>
43
44
<blockquoteclass="
44
45
warning "><strong>Warning</strong> Please, note we won't be implementing a fully-featured Google Cloud Pub/Sub server in this chapter as this would require diving into transporter specific technical details.
45
46
</blockquote>
@@ -53,7 +54,8 @@ <h4 appAnchor id="creating-a-strategy"><span>Creating a strategy</span></h4>
53
54
{
54
55
strategy: new GoogleCloudPubSubServer(),
55
56
},
56
-
);</code></pre>
57
+
);
58
+
</code></pre>
57
59
<p>Basically, instead of passing the normal transporter options object with <code>transport</code> and <code>options</code> properties, we pass a single property, <code>strategy</code>, whose value is an instance of our custom transporter class.</p>
58
60
<p>Back to our <code>GoogleCloudPubSubServer</code> class, in a real-world application, we would be establishing a connection to our message broker/external service and registering subscribers/listening to specific channels in <code>listen()</code> method (and then removing subscriptions & closing the connection in the <code>close()</code> teardown method),
59
61
but since this requires a good understanding of how Nest microservices communicate with each other, we recommend reading this <arel='nofollow' target='_blank' href="https://dev.to/nestjs/part-1-introduction-and-setup-1a2l">article series</a>.
@@ -63,17 +65,20 @@ <h4 appAnchor id="creating-a-strategy"><span>Creating a strategy</span></h4>
63
65
@MessagePattern('echo')
64
66
echo(@Payload() data: object) {
65
67
return data;
66
-
}</code></pre>
68
+
}
69
+
</code></pre>
67
70
<p>This message handler will be automatically registered by Nest runtime. With <code>Server</code> class, you can see what message patterns have been registered and also, access and execute the actual methods that were assigned to them.
68
71
To test this out, let's add a simple <code>console.log</code> inside <code>listen()</code> method before <code>callback</code> function is called:</p>
69
72
<pre><codeclass="language-typescript">
70
73
listen(callback: () => void) {
71
74
console.log(this.messageHandlers);
72
75
callback();
73
-
}</code></pre>
76
+
}
77
+
</code></pre>
74
78
<p>After your application restarts, you'll see the following log in your terminal:</p>
info "><strong>Hint</strong> If we used the <code>@EventPattern</code> decorator, you would see the same output, but with the <code>isEventHandler</code> property set to <code>true</code>.
79
84
</blockquote>
@@ -84,10 +89,12 @@ <h4 appAnchor id="creating-a-strategy"><span>Creating a strategy</span></h4>
<p>Once we execute the <code>echoHandler</code> passing an arbitrary string as an argument (<code>"Hello world!"</code> here), we should see it in the console:</p>
89
95
<pre><codeclass="language-json">
90
-
Hello world!</code></pre>
96
+
Hello world!
97
+
</code></pre>
91
98
<p>Which means that our method handler was properly executed.</p>
<p>As we mentioned in the first section, you don't necessarily need to use the <code>@nestjs/microservices</code> package to create microservices, but if you decide to do so and you need to integrate a custom strategy, you will need to provide a "client" class too.</p>
warning "><strong>Warning</strong> Please, note we won't be implementing a fully-featured Google Cloud Pub/Sub client in this chapter as this would require diving into transporter specific technical details.
<p>With this in place, let's create an instance of <code>GoogleCloudPubSubClient</code> class and run the <code>send()</code> method (which you might have seen in earlier chapters), subscribing to the returned observable stream.</p>
145
154
<pre><codeclass="language-typescript">
146
155
const googlePubSubClient = new GoogleCloudPubSubClient();
Hello world! // <-- after 5 seconds</code></pre>
164
+
Hello world! // <-- after 5 seconds
165
+
</code></pre>
155
166
<p>To test if our "teardown" method (which our <code>publish()</code> method returns) is properly executed, let's apply a timeout operator to our stream, setting it to 2 seconds to make sure it throws earlier then our <code>setTimeout</code> calls the <code>callback</code> function.</p>
156
167
<pre><codeclass="language-typescript">
157
168
const googlePubSubClient = new GoogleCloudPubSubClient();
0 commit comments