[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) }

 

posted @   Zhentiw  阅读(105)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-06-23 [Webpack 2] Chunking common modules from multiple apps with the Webpack CommonsChunkPlugin
2016-06-23 [Webpack 2] Grouping vendor files with the Webpack CommonsChunkPlugin
2013-06-23 【PHP 】 伪静态 - 3. 伪静态的基本使用
2013-06-23 【PHP 】伪静态 - 4. 实际运用
2013-06-23 【大型网站架构 原理】4. 网络协议的几个基本概念 (OSI7层模型,交换机,路由器的原理,ARP和代理ARP的原理)
点击右上角即可分享
微信分享提示