【发布时间】:2022-01-31 09:36:22
【问题描述】:
我正在使用 express/nodejs 和无服务器使用 AWS Lambda
但是,无服务器(无服务器离线)匹配我的 URL 的完整路径并且可以处理参数。然后将其传递给我的处理程序,该处理程序使用 express 来解析路径。但是,快速路由器不再接收完整路径 - 匹配的参数正在被丢弃。
我的 .yml 文件中有以下配置:
functions:
api:
handler: lambda.universal
events:
- httpApi:
method: '*'
path: /api
- httpApi:
method: '*'
path: /api/{params}
这是处理程序:
export const universal = (event: APIGatewayProxyEvent, context: Context) =>
{
awsServerlessExpress.proxy(server, event, context);
}
到这个路由器(为了简洁,我删除了一些东西):
router.get("/:param", async (req: Request, res: Response) => {
console.log("get with param hit");
const response = getNode(); // defined elsewhere
res.status(200).send(response);
});
router.get("/", async (req: Request, res: Response) => {
console.log("get hit (no param)");
const response = getNode(); // defined elsewhere
res.status(200).send(response);
});
当我调用任一 URL(即 localhost:3000/api/ 或 localhost:3000/api/parameter-here)时,它仍然会到达这些路由中的第二个并打印“get hit (no param)” .我还查看了路由器接收的参数(req 和 res),它不包含完整路径。 req.path 总是只是'/',req.paramas 总是{},req.route 包含东西但没有用,req.url 只是'/'。
有没有办法让无服务器接收到的 {params} 传递到我的路由器的路由中?
谢谢!
【问题讨论】:
标签: node.js express aws-lambda serverless