NodeJS 如何在 lambda Serverless Websocket 中获取请求对象?
NodeJS How to get request object in lambda Serverless Websocket?
我的客户端应用程序将 Websocket 服务器与 url 连接:
wss://xxxxxxx/xxxxx/xxxx?value=abcd
我的 websocket 服务器需要获取客户端在请求 url 中传递的值 "abcd",但是,我发现我在 NodeJS 服务器端处理程序代码中的所有内容是:
exports.handler = async function (event, context) {
const {
body,
requestContext: {
connectionId,
routeKey
}
} = event;
switch (routeKey) {
case '$connect':
......
问题是:在我的 $connect 块中,如何获取该查询字符串?
event
是一个APIGatewayProxyEvent
对象,那么你不能从event
对象获取查询参数。
对于你的案例,代码将是这样的:
...
const {
queryStringParameters, // here
body,
requestContext: {
connectionId,
routeKey
}
} = event;
const value = queryStringParameters.value || ''; // value will be `abcd` with url is wss://xxxxxxx/xxxxx/xxxx?value=abcd
...
我的客户端应用程序将 Websocket 服务器与 url 连接: wss://xxxxxxx/xxxxx/xxxx?value=abcd
我的 websocket 服务器需要获取客户端在请求 url 中传递的值 "abcd",但是,我发现我在 NodeJS 服务器端处理程序代码中的所有内容是:
exports.handler = async function (event, context) {
const {
body,
requestContext: {
connectionId,
routeKey
}
} = event;
switch (routeKey) {
case '$connect':
......
问题是:在我的 $connect 块中,如何获取该查询字符串?
event
是一个APIGatewayProxyEvent
对象,那么你不能从event
对象获取查询参数。
对于你的案例,代码将是这样的:
...
const {
queryStringParameters, // here
body,
requestContext: {
connectionId,
routeKey
}
} = event;
const value = queryStringParameters.value || ''; // value will be `abcd` with url is wss://xxxxxxx/xxxxx/xxxx?value=abcd
...