Skip to content

Commit c54ed6f

Browse files
committed
Example API structure
1 parent d8c092e commit c54ed6f

File tree

8 files changed

+97
-90
lines changed

8 files changed

+97
-90
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.dynamodb/
2+
node_modules/

app.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const express = require('express');
2+
const bodyParser = require('body-parser');
3+
4+
const app = express();
5+
app.use(bodyParser.json({ strict: false }));
6+
7+
module.exports = app;

db.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"This-is-mock-data-only-and-can-be-deleted": "This has nothing to do with dynamoDb",
23
"events": [
34
{
45
"id": "1",

dynamoDb.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const AWS = require('aws-sdk');
2+
3+
const IS_OFFLINE = 'true'; // process.env.IS_OFFLINE
4+
5+
let dynamoDb;
6+
if (IS_OFFLINE === 'true') {
7+
dynamoDb = new AWS.DynamoDB.DocumentClient({
8+
region: 'localhost',
9+
endpoint: 'http://localhost:8000',
10+
accessKeyId: 'test',
11+
secretAccessKey: 'test'
12+
})
13+
console.log(dynamoDb);
14+
} else {
15+
dynamoDb = new AWS.DynamoDB.DocumentClient();
16+
};
17+
18+
module.exports = dynamoDb;

index.js

Lines changed: 0 additions & 83 deletions
This file was deleted.

routes/users/create.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const serverless = require('serverless-http');
2+
const dynamoDb = require('../../dynamoDb');
3+
const app = require('../../app');
4+
5+
// createUser
6+
7+
const USERS_TABLE = process.env.USERS_TABLE;
8+
9+
app.post('/users', function (req, res) {
10+
const { userId, name } = req.body;
11+
if (typeof userId !== 'string') {
12+
res.status(400).json({ error: '"userId" must be a string' });
13+
} else if (typeof name !== 'string') {
14+
res.status(400).json({ error: '"name" must be a string' });
15+
}
16+
17+
const params = {
18+
TableName: USERS_TABLE,
19+
Item: {
20+
userId: userId,
21+
name: name,
22+
},
23+
};
24+
25+
dynamoDb.put(params, (error) => {
26+
if (error) {
27+
console.log(error);
28+
res.status(400).json({ error: 'Could not create user' });
29+
}
30+
res.json({ userId, name });
31+
});
32+
})
33+
34+
module.exports.handler = serverless(app);

routes/users/get.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const serverless = require('serverless-http');
2+
const dynamoDb = require('../../dynamoDb');
3+
const app = require('../../app');
4+
5+
// getUser
6+
7+
const USERS_TABLE = process.env.USERS_TABLE;
8+
9+
app.get('/users/:userId', function (req, res) {
10+
const params = {
11+
TableName: USERS_TABLE,
12+
Key: {
13+
userId: req.params.userId,
14+
},
15+
}
16+
17+
dynamoDb.get(params, (error, result) => {
18+
if (error) {
19+
console.log(error);
20+
res.status(400).json({ error: 'Could not get user' });
21+
}
22+
if (result.Item) {
23+
const {userId, name} = result.Item;
24+
res.json({ userId, name });
25+
} else {
26+
res.status(404).json({ error: "User not found" });
27+
}
28+
});
29+
})
30+
31+
module.exports.handler = serverless(app);

serverless.yml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,14 @@ provider:
3434
USERS_TABLE: ${self:custom.tableName}
3535

3636
functions:
37-
app:
38-
handler: index.handler
39-
events:
40-
- http: ANY /
41-
- http: 'ANY {proxy+}'
37+
4238
getUser:
43-
handler: index.handler
39+
handler: routes/users/get.handler
4440
events:
4541
- http: 'GET /users/{proxy+}'
42+
4643
createUser:
47-
handler: index.handler
44+
handler: routes/users/create.handler
4845
events:
4946
- http: 'POST /users'
5047

0 commit comments

Comments
 (0)