[AWS Lambda] Convert a Express node.js app to serverless
Install new dependencies
In a folder with the serverless project run the following commands to add new dependencies:
npm install --save aws-serverless-express
npm install --save express
express
- is a very popular Node.js web framework that we will use in this lesson aws-serverless-express
- is a library that allows using express
with AWS Lambda
Import new dependencies
In the getGroups.ts
you need to import the following dependencies:
import * as express from 'express'
import * as awsServerlessExpress from 'aws-serverless-express'
Create an Express instance
Once the dependencies are imported you need to create an Express application:
const app = express()
Add a handler for a GET method
To define how to process an incoming GET
request, we need to use the app.get
method and pass a function that will be called to process a request:
app.get('/groups', async (_req, res) => {
// TODO: get all groups as before
const groups = ...
// Return a list of groups
res.json({
items: groups
})
})
You can read more about how to use Express here.
To return a JSON response we use the .json()
method on the response object.
Export a Lambda handler
Now the last thing that we need to do is to create a Lambda handler. To do this you can use the following code snippet:
// Create Express server
const server = awsServerlessExpress.createServer(app)
// Pass API Gateway events to the Express server
exports.handler = (event, context) => { awsServerlessExpress.proxy(server, event, context) }
相关文章
- js书写原生ajax,JS 原生ajax写法
- html如何只刷新页面指定,js控制页面刷新 JS刷新当前页面的几种方法总结
- AWS攻略——一文看懂AWS IAM设计和使用
- JS面试题-js新增基本数据类型BigInt
- JS对象转数组_js怎么把数组转成对象
- Matano:一款针对AWS的开源安全湖平台
- 使用QuTrunk+AWS ParallelCluster3进行并行计算
- 芯片创新如何成为AWS成功背后的 "秘诀"
- AWS教程—解决网站加载缓慢的难题
- 实战教程:在AWS上构建一个情感分析「机器人」
- vue main.js中app绑定的是index.html还是App.vue中详解编程语言
- 创建JS文件:在Linux下实现自动化任务(linux创建js文件)
- 揭秘亚马逊新帅的Chop会议和Nice邮件 贾西之后AWS CEO花落谁家?
- Linux上的JS压缩工具(js压缩工具linux)
- AWS 将提供 8TB 或 16TB 内存的新型实例
- 上万个公司都在用的机器学习服务SageMaker,AWS宣布要在中国推出了
- Amazon SageMaker加持,网约车领域开始有AWS智能语音方案了
- AWS中国全新上线两项文件存储服务,集齐存储服务“全家桶”
- 数据库使用AWS连接本地MySQL数据库(aws调用本地mysql)
- 深入研究AWS如何调用MySQL(aws调用mysql)
- 云端无限存储AWS大规模MySQL数据库管理(aws海量数据mysql)
- AWS改变MySQL配置,创建更加美好的未来(aws改mysql配置)
- 域跨越地域的赋能利用AWSMysql实现多区域云数据库部署(aws mysql 多区)
- AWS取代Oracle,重新定义数据库技术(AWS代替Oracle)
- Redis中的订阅机制及其在JS中的应用(redis 订阅 js)