File tree Expand file tree Collapse file tree 8 files changed +97
-90
lines changed
Expand file tree Collapse file tree 8 files changed +97
-90
lines changed Original file line number Diff line number Diff line change 1+ .dynamodb /
2+ node_modules /
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 11{
2+ "This-is-mock-data-only-and-can-be-deleted" : " This has nothing to do with dynamoDb" ,
23 "events" : [
34 {
45 "id" : " 1" ,
Original file line number Diff line number Diff line change 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 ;
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 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 ) ;
Original file line number Diff line number Diff line change 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 ) ;
Original file line number Diff line number Diff line change @@ -34,17 +34,14 @@ provider:
3434 USERS_TABLE : ${self:custom.tableName}
3535
3636functions :
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
You can’t perform that action at this time.
0 commit comments