admin管理员组文章数量:1122832
I have an Azure function where I am processing a large number files, I am aiming to send to progress messages to the browser in html to report on progress. I am using nextjs - below is the controller which works fine locally.
@Get('ping-html')
pingHTML(@Res() response: Response) {
response.status(HttpStatus.PARTIAL_CONTENT);
response.setHeader('Content-Type', 'text/html');
response.setHeader('X-Content-Type-Options', 'nosniff');
response.setHeader('Transfer-Encoding', 'chunked');
const html = `<html><h1>Controller alive and kicking ...${moment().utc()}</h1>`;
console.log(html);
response.write(html);
setTimeout(()=>{
console.log('second time');
response.write(`Second line after timeout ${moment().utc()}`);
response.write("</body></html>")
response.end();
},10000)
console.log(" response constructed ");
return html;
}
But when deploy the code on Azure, it doesn't render the html and eventually times out with an error.
GET http://localhost:4000/api/ping-html net::ERR_INCOMPLETE_CHUNKED_ENCODING 206 (Partial Content)
I have followed this blog / announcement
main/index.ts
:
import { app, HttpRequest } from '@azure/functions';
export default function(context: any, req: HttpRequest): void {
app.setup({ enableHttpStream: true })
AzureHttpAdapter.handle(createApp, context, req);
console.log("Deamon awake -- Azure Function started ...");
}
Any suggestions on resolving the above issue or alternatives of reporting progress from an Azure function. Thanks
I have an Azure function where I am processing a large number files, I am aiming to send to progress messages to the browser in html to report on progress. I am using nextjs - below is the controller which works fine locally.
@Get('ping-html')
pingHTML(@Res() response: Response) {
response.status(HttpStatus.PARTIAL_CONTENT);
response.setHeader('Content-Type', 'text/html');
response.setHeader('X-Content-Type-Options', 'nosniff');
response.setHeader('Transfer-Encoding', 'chunked');
const html = `<html><h1>Controller alive and kicking ...${moment().utc()}</h1>`;
console.log(html);
response.write(html);
setTimeout(()=>{
console.log('second time');
response.write(`Second line after timeout ${moment().utc()}`);
response.write("</body></html>")
response.end();
},10000)
console.log(" response constructed ");
return html;
}
But when deploy the code on Azure, it doesn't render the html and eventually times out with an error.
GET http://localhost:4000/api/ping-html net::ERR_INCOMPLETE_CHUNKED_ENCODING 206 (Partial Content)
I have followed this blog / announcement
https://techcommunity.microsoft.com/blog/appsonazureblog/azure-functions-support-for-http-streams-in-node-js-is-now-in-preview/4066575
main/index.ts
:
import { app, HttpRequest } from '@azure/functions';
export default function(context: any, req: HttpRequest): void {
app.setup({ enableHttpStream: true })
AzureHttpAdapter.handle(createApp, context, req);
console.log("Deamon awake -- Azure Function started ...");
}
Any suggestions on resolving the above issue or alternatives of reporting progress from an Azure function. Thanks
Share Improve this question edited Nov 22, 2024 at 4:29 marc_s 754k183 gold badges1.4k silver badges1.5k bronze badges asked Nov 21, 2024 at 21:41 maxkartmaxkart 6675 silver badges22 bronze badges 2- Did you try increasing functionTimeout? – Pravallika KV Commented Nov 22, 2024 at 3:32
- not facing a timeout issue, the sample function doesnt work even when i reduce setTimeout to a second – maxkart Commented Nov 22, 2024 at 10:47
1 Answer
Reset to default 0I have created a NestJs serverless Azure function to process large number files.
main/index.ts:
import { app, HttpRequest } from '@azure/functions';
import { AzureHttpAdapter } from '@nestjs/azure-func-http';
import { createApp } from 'src/main.azure';
export default function(context: any, req: HttpRequest): void {
app.setup({ enableHttpStream: true })
AzureHttpAdapter.handle(createApp, context, req);
console.log("Deamon awake -- Azure Function started ...");
}
src/app.controller.ts:
import { Controller, Get, HttpStatus, Res } from '@nestjs/common';
import { Response } from 'express';
import * as moment from 'moment';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
@Get('ping-html')
pingHTML(@Res() response: Response) {
response.status(HttpStatus.PARTIAL_CONTENT);
response.setHeader('Content-Type', 'text/html');
response.setHeader('X-Content-Type-Options', 'nosniff');
response.setHeader('Transfer-Encoding', 'chunked');
const html = `<html><h1>Controller alive and kicking ...${moment().utc()}</h1>`;
console.log(html);
response.write(html);
setTimeout(()=>{
console.log('second time');
response.write(`Second line after timeout ${moment().utc()}`);
response.write("</body></html>")
response.end();
},10000)
console.log(" response constructed ");
return html;
}
}
Console Output:
Functions:
main: http://localhost:7071/api/{*segments}
For detailed output, run func with --verbose flag.
[2024-11-26T08:10:36.060Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2024-11-26T08:10:46.723Z] Executing 'Functions.main' (Reason='This function was programmatically called via the host APIs.', Id=34326561-d942-4115-af77-3f3035384d95)
[2024-11-26T08:10:47.154Z] Deamon awake -- Azure Function started ...
[2024-11-26T08:10:47.173Z] [Nest] 9868 - 11/26/2024, 1:40:47 PM LOG [NestFactory] Starting Nest application...
[2024-11-26T08:10:47.190Z] [Nest] 9868 - 11/26/2024, 1:40:47 PM LOG [InstanceLoader] AppModule dependencies initialized +36ms
[2024-11-26T08:10:47.196Z] [Nest] 9868 - 11/26/2024, 1:40:47 PM LOG [RoutesResolver] AppController {/api}: +6ms
[2024-11-26T08:10:47.199Z] [Nest] 9868 - 11/26/2024, 1:40:47 PM LOG [RouterExplorer] Mapped {/api, GET} route +4ms
[2024-11-26T08:10:47.202Z] [Nest] 9868 - 11/26/2024, 1:40:47 PM LOG [RouterExplorer] Mapped {/api/ping-html, GET} route +1ms
[2024-11-26T08:10:47.206Z] [Nest] 9868 - 11/26/2024, 1:40:47 PM LOG [NestApplication] Nest application successfully started +5ms
[2024-11-26T08:10:47.212Z] <html><h1>Controller alive and kicking ...Tue Nov 26 2024 08:10:47 GMT+0000</h1>
[2024-11-26T08:10:47.216Z] response constructed
[2024-11-26T08:10:57.218Z] second time
[2024-11-26T08:10:57.341Z] Executed 'Functions.main' (Succeeded, Id=34326561-d942-4115-af77-3f3035384d95, Duration=10644ms)
Portal:
Function App Logs:
2024-11-26T11:30:52Z [Verbose] Request successfully matched the route with name 'main' and template 'api/{*segments}'
2024-11-26T11:30:52Z [Information] Executing 'Functions.main' (Reason='This function was programmatically called via the host APIs.', Id=8166b9b2-02cd-47ee-bc22-26e3e7fa0b73)
2024-11-26T11:30:52Z [Verbose] Sending invocation id: '8166b9b2-02cd-47ee-bc22-26e3e7fa0b73
2024-11-26T11:30:52Z [Verbose] Posting invocation id:8166b9b2-02cd-47ee-bc22-26e3e7fa0b73 on workerId:5be600cb-2744-49c1-ba23-3f559eaa8401
2024-11-26T11:30:52Z [Information] Deamon awake -- Azure Function started ...
2024-11-26T11:30:52Z [Information] <html><h1>Controller alive and kicking ...Tue Nov 26 2024 11:30:51 GMT+0000</h1>
2024-11-26T11:30:52Z [Information] response constructed
2024-11-26T11:31:02Z [Information] second time
2024-11-26T11:31:02Z [Information] Executed 'Functions.main' (Succeeded, Id=8166b9b2-02cd-47ee-bc22-26e3e7fa0b73, Duration=10011ms)
本文标签: streamAzure functionNestjs Chunked HTML responseStack Overflow
版权声明:本文标题:stream - Azure function - Nestjs Chunked HTML response - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307014a1933166.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论