首页 > 解决方案 > 如何使用 AWS post lambda 打开 Web 浏览器

问题描述

我写了下面的代码:

static async postSearchResult(httpContext: HttpContext, injector: Injector) {
    const log = injector.get(Log);
    const service = injector.get(Service);
    try {
        let result = await service.redirectToUI(JSON.parse(httpContext.getRequestBody()));

        httpContext.ok(result, 200, {'Content-Type': 'application/json'});
    } catch (e) {
        httpContext.fail(e, 500);
    }
}

protected redirectToUI(response: any) {
    // If any post api call happened then it should open web browser and pass some field as query parameter
    window.open("https://www.google.com?abc=response.abc");
    return response ? response : "failed";
}

在这里,我收到以下错误:

执行失败 ReferenceError: 未定义窗口

我究竟做错了什么?

标签: node.jsamazon-web-servicespostaws-lambda

解决方案


你试图完成的事情没有多大意义。Lambda 是一个后端服务。要打开新的浏览器窗口,您需要使用前端 JavaScript,而不是后端 Node(在后端,您无法访问前端window对象)。

如果您想打开一个新的浏览器窗口作为对某些后端响应的反应,那么您可以在 HTTP 响应中发送一些指示符(即shouldOpenNewWindow: true作为响应对象的一部分),在前端解析该响应,然后它指示器存在,然后您可以发出window.open命令。但它必须在前端完成。


推荐阅读