-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (57 loc) · 2.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Import required dependency packages
const serverless = require('@vendia/serverless-express');
const express = require('express');
const { DynamoUser } = require('./dynamodb'); // dynamodb database
const { names } = require('./constants');
// Serverless Express setup
const app = express();
const lambdaHandler = serverless({ app });
// API Endpoints
app.get('/', (_, res) => {
res.send('Welcome to the Lambda Serverless Express app with DynamoDB. In this sample code, you can create and list users.');
});
// Endpoint to create a user
app.get('/create', async (_, res) => {
try {
const randomName = getRandomName();
const uniqueId = generateUniqueId(randomName);
const user = await DynamoUser.create({ id: uniqueId, name: randomName });
res.send(`User created: ${JSON.stringify(user)}`);
} catch (error) {
res.status(500).send(`Failed to create user: ${error}`);
}
});
// Endpoint to list a random number of users between 0 and 50
app.get('/list', async (_, res) => {
try {
const randomLimit = getRandomLimit();
const users = await DynamoUser.list({ limit: randomLimit });
console.log(`Fetched ${users.length} users`);
res.send(`${JSON.stringify(users)} Fetched ${users.length} users`);
} catch (error) {
console.error('Failed to fetch users:', error);
res.status(500).send(`Failed to fetch users: ${error}`);
}
});
// Helper functions
function getRandomName() {
const randomIndex = Math.floor(Math.random() * names.length);
return names[randomIndex];
}
function generateUniqueId(name) {
return `${name.slice(0, 2)}-${Date.now()}`;
}
function getRandomLimit() {
return Math.floor(Math.random() * 50);
}
// AWS Lambda Handler
module.exports.handler = async (event, context) => {
return lambdaHandler(event, context);
};
// Local server for development
// if (process.env.NODE_ENV !== 'AWS') {
// const port = 3001;
// app.listen(port, () => {
// console.log(`Server running on http://localhost:${port}/`);
// });
// }