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.
If you plan to stream large amounts of data, adjust the app setting FUNCTIONS_REQUEST_BODY_SIZE_LIMIT
in Azure or in your local.settings.json
file. The default value is 104857600
, i.e., limiting your request to 100mb maximum.
Add the following code to your app in any file included by your main field.
const { app } = require('@azure/functions');
app.setup({ enableHttpStream: true });
import { app } from '@azure/functions';
app.setup({ enableHttpStream: true });
That's it! The existing HttpRequest
and HttpResponse
types in programming model v4 already support many ways of handling the body, including as a stream. Use request.body
to truly benefit from streams, but rest assured you can continue to use methods like request.text()
which will always return the body as a string.
const { app } = require('@azure/functions');
const { createWriteStream } = require('fs');
const { Writable } = require('stream');
app.http('httpTriggerStreamRequest', {
methods: ['POST'],
authLevel: 'anonymous',
handler: async (request, context) => {
const writeStream = createWriteStream('<output file path>');
await request.body.pipeTo(Writable.toWeb(writeStream));
return { body: 'Done!' };
},
});
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions';
import { createWriteStream } from 'fs';
import { Writable } from 'stream';
export async function httpTriggerStreamRequest(
request: HttpRequest,
context: InvocationContext
): Promise<HttpResponseInit> {
const writeStream = createWriteStream('<output file path>');
await request.body.pipeTo(Writable.toWeb(writeStream));
return { body: 'Done!' };
}
app.http('httpTriggerStreamRequest', {
methods: ['POST'],
authLevel: 'anonymous',
handler: httpTriggerStreamRequest,
});
Below is an example of an HTTP triggered function that streams a file's content as the response to incoming HTTP GET requests:
JavaScript
const { app } = require('@azure/functions');
const { createReadStream } = require('fs');
app.http('httpTriggerStreamResponse', {
methods: ['GET'],
authLevel: 'anonymous',
handler: async (request, context) => {
const body = createReadStream('<input file path>');
return { body };
},
});
TypeScript
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions';
import { createReadStream } from 'fs';
export async function httpTriggerStreamResponse(
request: HttpRequest,
context: InvocationContext
): Promise<HttpResponseInit> {
const body = createReadStream('<input file path>');
return { body };
}
app.http('httpTriggerStreamResponse', {
methods: ['GET'],
authLevel: 'anonymous',
handler: httpTriggerStreamResponse,
});
For a ready-to-run app with more detailed code, check out this GitHub repo.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.