본문 바로가기

Serverless

[Serverless] 2. serverless에서 express 사용하기

1. express 설치

# npm install express --save

2. serverless-express 설치

# npm install aws-serverless-express

3. handler 설정

'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app')
const server = awsServerlessExpress.createServer(app)

exports.handler = (event, context) => { awsServerlessExpress.proxy(server, event, context) }

4. middleware 설정

const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
app.use(awsServerlessExpressMiddleware.eventContext())
app.get('/', (req, res) => {
  res.json(req.apiGateway.event)
})

 

출처 : https://www.npmjs.com/package/aws-serverless-express

'Serverless' 카테고리의 다른 글

[Serverless] 1. serverless 시작하기 및 local 환경 설정  (0) 2021.12.03