Back to home

aws-lambda/

node-apigw

Version: 0.1.2

Last Updated: Unknown


Aws-lambda - Node-apigw

Use this tempalate to A starter for node apigw

Required

TODO: Update this section

VariableData TypeInfo
typestringThe name of the Lift folder type
pkgstringThe name of the package

Module files

1import { Handler } from "aws-lambda"
2
3const handleError = (err) => {
4 const body = err.stack || JSON.stringify(err, null, 2)
5 console.error(body)
6
7 return {
8 statusCode: 500,
9 body: "Internal server error",
10 }
11}
12
13const handleGet = async (event, _context) => {
14 try {
15 console.log(event)
16
17 return {
18 statusCode: 200,
19 headers: {
20 "content-type": "application/json",
21 },
22 body: JSON.stringify({
23 message: "Hello, world!",
24 }),
25 }
26 } catch (err) {
27 return handleError(err)
28 }
29}
30
31const handlePost = async (event, _context) => {
32 try {
33 const { email } = event.body
34
35 if (!email) {
36 return {
37 statusCode: 406,
38 body: "Missing required parameters",
39 }
40 }
41
42 // do something
43 const res = {}
44
45 return {
46 statusCode: 200,
47 headers: {
48 "content-type": "application/json",
49 },
50 body: JSON.stringify(res),
51 }
52 } catch (err) {
53 return handleError(err)
54 }
55}
56
57const handler: Handler = async (event, context) => {
58 try {
59 switch (event.httpMethod) {
60 case "GET":
61 return await handleGet(event, context)
62 case "POST":
63 return await handlePost(event, context)
64 default:
65 return {
66 statusCode: 405,
67 body: "Not allowed",
68 }
69 }
70 } catch (error) {
71 var body = error.stack || JSON.stringify(error, null, 2)
72 console.error(body)
73
74 return {
75 statusCode: 500,
76 headers: {},
77 }
78 }
79}
80
81exports.handler = handler

Install


Lift generate


Repository

https://github.com/okeeffed/pkg-lift-aws-lambda

Sections