This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Community Hub.
Being one of the most requested features for Azure Functions, we’re confident that this release will unblock Node.js Functions customers and make scenarios like processing large data, streaming OpenAI responses, delivering dynamic content etc. possible. Customers can leverage this feature for use cases where real time exchange and interaction between client and server over HTTP connections is needed. We recommend using streams to get the best performance and reliability for your app.
HTTP Streams in Node.js is supported only in the Azure Functions Node.js v4 programming model. Follow these instructions to try out HTTP Streams for your Node.js apps.
Prerequisites
- Version 4 of the Node.js programming model. Learn more about the differences between v3 and v4 in the migration guide.
- Version 4.3.0 or higher of the @azure/functions npm package.
- If running in Azure, version 4.28 of the Azure Functions runtime.
- If running locally, version 4.0.5530 of Azure Functions Core Tools.
Steps
-
If you plan to stream large amounts of data, adjust the app setting
FUNCTIONS_REQUEST_BODY_SIZE_LIMITin Azure or in yourlocal.settings.jsonfile. The default value is104857600, i.e., limiting your request to 100mb maximum. -
Add the following code to your app in any file included by your main field.
JavaScript
const { app } = require('@azure/functions'); app.setup({ enableHttpStream: true });
TypeScriptimport { app } from '@azure/functions'; app.setup({ enableHttpStream: true }); -
That's it! The existing
HttpRequestandHttpResponsetypes in programming model v4 already support many ways of handling the body, including as a stream. Userequest.bodyto truly benefit from streams, but rest assured you can continue to use methods likerequest.text()which will always return the body as a string.
Example code
TypeScript
Below is an example of an HTTP triggered function that streams a file's content as the response to incoming HTTP GET requests:
JavaScript
TypeScript
Sample code
For a ready-to-run app with more detailed code, check out this GitHub repo.
Known issues
The request.params object will be an empty object. The suggested workaround is to use a package like path-to-regexp to parse the request.url property. Keep in mind that Azure Functions only supports ASP.NET Core route templates (i.e. user/{name}) when registering your function, so you will likely need to use a different syntax (i.e. /user/:name) when parsing the url.
Here is example code using the suggested workaround:
