node.js - 如何使用 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: 未定义窗口
我究竟做错了什么?
解决方案
你试图完成的事情没有多大意义。Lambda 是一个后端服务。要打开新的浏览器窗口,您需要使用前端 JavaScript,而不是后端 Node(在后端,您无法访问前端window
对象)。
如果您想打开一个新的浏览器窗口作为对某些后端响应的反应,那么您可以在 HTTP 响应中发送一些指示符(即shouldOpenNewWindow: true
作为响应对象的一部分),在前端解析该响应,然后它指示器存在,然后您可以发出window.open
命令。但它必须在前端完成。
推荐阅读
- docker - 使用 Prometheus 监控 bash 命令或 shell 脚本的结果
- docker - GnuTLS:TLS 连接未正确终止。无法建立 SSL 连接
- arrays - 我对这些情况的时间复杂度是正确的?
- modelica - 模拟后如何自动删除 Dymolas 构建文件?
- kotlin - onMapReady 函数似乎没有在 android 应用程序上调用
- r - 如何根据曝光调整模型中的不同协变量?
- r - 将日期列合并为一个
- next.js - 在 WriteStream 实例上发出“错误”事件
- java - java hashmap bucket真的包含一个列表吗?
- quartz.net - 嗨,为什么应用程序进入睡眠状态时石英不可用?