Azure 函数 - Nestjs 分块 HTML 响应

问题描述 投票:0回答:1

我有一个 Azure 函数,正在处理大量文件,我的目标是以 html 形式向浏览器发送进度消息以报告进度。我正在使用 nextjs - 下面是在本地工作正常的控制器。

@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;
}

但是当在 Azure 上部署代码时,它不会渲染 html,最终会超时并出现错误。

GET http://localhost:4000/api/ping-html net::ERR_INCOMPLETE_CHUNKED_ENCODING 206 (Partial Content)

我已关注此博客/公告

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 ...");
}

有关解决上述问题的任何建议或通过 Azure 函数报告进度的替代方案。谢谢

azure-functions stream nestjs
1个回答
0
投票

我创建了一个 NestJs 无服务器 Azure 函数来处理大量文件。

主/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;
}
}

控制台输出:

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)

传送门:

enter image description here

功能应用日志:

enter image description here

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)
© www.soinside.com 2019 - 2024. All rights reserved.