[英]Routing in a lambda function without API Gateway
I have an express app and is deployed via serverless.我有一个快速应用程序,并通过无服务器部署。 My stack is just a lambda function + API Gateway.
我的堆栈只是一个 lambda 函数 + API 网关。 That works and it's accessible on Postman or httpxmlrequest.
这有效并且可以在 Postman 或 httpxmlrequest 上访问。 But if I take out API Gateway piece from the stack, is it possible to still invoke this lambda function using the AWS SDK/cli, somehow do routing by passing in a
path (eg: /books)
and a method (eg: POST)
together with the payload?但是,如果我从堆栈中取出 API Gateway 部分,是否仍然可以使用 AWS SDK/cli 调用此 lambda 函数,通过传入
path (eg: /books)
和method (eg: POST)
以某种方式进行路由连同有效载荷?
I'm new to the AWS ecosystem... just started using serverless + node.我是 AWS 生态系统的新手……刚开始使用无服务器 + 节点。
Say I have a simple express app like so:假设我有一个简单的 Express 应用程序,如下所示:
const serverless = require('serverless-http');
const express = require('express');
const bodyParser = require('body-parser');
const helpersRoute = require('./routes');
const booksRoute = require('./routes/books');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/', helpersRoute);
app.use('/books', booksRoute);
module.exports.handler = serverless(app);
And this is my serverless config:这是我的无服务器配置:
service: service-test
provider:
name: aws
runtime: nodejs8.10
stage: dev
functions:
app:
handler: index.handler
Yes, it is possible by using the Lambda SDK.是的,可以使用 Lambda SDK。 Since you are running your function behind an express server though, you will have to pass an event which looks exactly like API Gateway's, so, from the invoked Lambda perspective, it would simply be an API Gateway's call.
但是,由于您在 express 服务器后面运行您的函数,因此您必须传递一个与 API Gateway 完全一样的事件,因此,从调用的 Lambda 的角度来看,它只是一个 API Gateway 的调用。
You can check the docs to see what an API Gateway event looks like, but, essentially, you only need path and body (in case of POST, PUT, PATCH) requests .您可以查看文档以查看 API 网关事件是什么样的,但本质上,您只需要路径和正文(在 POST、PUT、PATCH 的情况下)请求。 If you need query params, etc you can also pass them along.
如果您需要查询参数等,您也可以传递它们。
Make sure you set the InvocationType
to RequestResponse
确保将
InvocationType
设置为RequestResponse
Here's a sample in Node.js
:这是
Node.js
的一个示例:
const lambda = new AWS.Lambda();
await lambda
.invoke({
FunctionName: 'FunctionName',
InvocationType: 'RequestResponse',
Payload: JSON.stringify({
path: '/your_path',
}),
})
.promise();
EDIT: The OP was having problems with the way the data comes from a given Lambda function to the Express-based Lambda function, so I decided to add my own configuration for comparison purposes:编辑: OP 在数据从给定 Lambda 函数到基于 Express 的 Lambda 函数的方式方面存在问题,因此我决定添加自己的配置以进行比较:
const serverless = require('serverless-http');
const express = require('express');
const cors = require('cors');
const app = express();
const bodyParser = require('body-parser');
const routes = require('./routes');
app.use(cors());
app.use(bodyParser.json({ strict: false }));
app.use('/api', routes);
module.exports.handler = serverless(app);
Sample controller method:示例控制器方法:
const fetchOne = async (req, res) =>
res
.status(200)
.json(await MyService.doSomething(req.body.someField));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.