⏱ 10 分钟教程:创建无服务器 Express Web 服务器和 API
无服务器函数最流行的用例之一是部署和运行带有路由功能的 Web 服务器。在本教程中,我将向您展示如何使用AWS Lambda、Amazon API Gateway和AWS Amplify在几分钟内完成部署并运行。
我将使用的库是专门为此用例制作的Serverless Express项目。
使用这个库,您可以轻松地将event
和代理context
到express服务器,从那里您可以访问不同的路由和 HTTP 方法,如get
、、和。post
put
delete
app.get('/', (req, res) => {
res.json(req.apiGateway.event)
})
入门
部署 Lambda 函数的方法有很多种,您可以直接进入 AWS 控制台,使用无服务器框架,或使用大量其他利用基础设施即代码的工具。
我将使用基于 CLI 的方法和Amplify Framework。
首先安装并配置 Amplify CLI。
要观看如何配置 CLI 的视频演示,请单击此处。
$ npm install -g @aws-amplify/cli
$ amplify configure
现在,使用您选择的 JavaScript 框架(React、Angular、Vue 等)创建一个项目。
$ npx create-react-app myapp
$ cd myapp
$ npm install aws-amplify
接下来,在 JS 项目的根目录中初始化一个新的 Amplify 项目:
$ amplify init
# Answer the questions prompted by the CLI
现在,我们可以创建 API 和 Web 服务器了。为此,我们可以使用 Amplifyadd
命令:
$ amplify add api
? Please select from one of the below mentioned services: REST
? Provide a friendly name for your resource to be used as a label for this category in the project: myapi
? Provide a path (e.g., /items): /items (or whatever path you would like)
? Choose a Lambda source: Create a new Lambda function
? Provide a friendly name for your resource to be used as a label for this category in the project: mylambda
? Provide the AWS Lambda function name: mylambda
? Choose the function template that you want to use: Serverless express function
? Do you want to access other resources created in this project from your Lambda function? N
? Do you want to edit the local lambda function now? N
? Restrict API access: N
? Do you want to add another path? N
CLI 为我们创建了一些东西:
- API 端点
- Lambda 函数
- 在函数中使用 Serverless Express 的 Web 服务器
/items
路线上不同方法的一些样板代码
让我们打开服务器的代码。
打开amplify/backend/function/mylambda/src/index.js。在这里,您将看到带有 的主函数处理程序,event
并被context
代理到位于 的 Express 服务器./app.js
:
const awsServerlessExpress = require('aws-serverless-express');
const app = require('./app');
const server = awsServerlessExpress.createServer(app);
exports.handler = (event, context) => {
console.log(`EVENT: ${JSON.stringify(event)}`);
awsServerlessExpress.proxy(server, event, context);
};
接下来,打开ampify/backend/function/mylambda/src/app.js。
在这里,您将看到 Express 服务器的代码,以及我们声明的路由的不同 HTTP 方法的一些样板。找到路由app.get('/items')
并将其更新为以下内容:
// amplify/backend/function/mylambda/src/app.js
app.get('/items', function(req, res) {
const items = ['hello', 'world']
res.json({ success: 'get call succeed!', items });
});
我们可以在部署之前在本地进行测试,但我们首先需要安装 Lambda 的依赖项:
$ cd amplify/backend/function/mylambda/src && npm install && cd ../../../../../
要调用该函数并启动服务器,请运行以下命令:
$ amplify function invoke mylambda
现在,服务器正在端口3000上运行,我们可以向其发出请求。要从命令行执行此操作,我们可以运行以下 curl 命令:
$ curl http://localhost:3000/items
# {"success":"get call succeed!","items":["hello","world"]}%
要部署 API 和功能,我们可以运行以下push
命令:
$ amplify push
现在,您可以从任何 JS 客户端开始与 API 进行交互:
// get request
const items = await API.get('myapi', '/items')
// post with data
const data = { body: { items: ['some', 'new', 'items'] } }
await API.post('myapi', '/items', data)
从这里开始,你可能需要添加其他路径。为此,请运行更新命令:
$ amplify update api
从那里,您可以添加、更新或删除路径。
有关 API 类别的更多信息,请单击此处。
文章来源:https://dev.to/aws/10-minute-tutorial-creating-a-serverless-express-web-server-api-29j7