|
| 1 | +<!-- |
| 2 | +title: 'AWS Serverless HTTP API with DynamoDB and offline support example in NodeJS' |
| 3 | +description: 'This example demonstrates how to run a service locally, using the ''serverless-offline'' plugin. It provides an HTTP API to manage Todos stored in DynamoDB.' |
| 4 | +layout: Doc |
| 5 | +framework: v1 |
| 6 | +platform: AWS |
| 7 | +language: nodeJS |
| 8 | +authorLink: 'https://github.com/adambrgmn' |
| 9 | +authorName: 'Adam Bergman' |
| 10 | +authorAvatar: 'https://avatars1.githubusercontent.com/u/13746650?v=4&s=140' |
| 11 | +--> |
| 12 | +# Serverless HTTP API with DynamoDB and offline support |
| 13 | + |
| 14 | +This example demonstrates how to run a service locally, using the |
| 15 | +[serverless-offline](https://github.com/dherault/serverless-offline) plugin. It |
| 16 | +provides an HTTP API to manage Todos stored in a DynamoDB, similar to the |
| 17 | +[aws-node-http-api-dynamodb](https://github.com/serverless/examples/tree/master/aws-node-http-api-dynamodb) |
| 18 | +example. A local DynamoDB instance is provided by the |
| 19 | +[serverless-dynamodb-local](https://github.com/99xt/serverless-dynamodb-local) |
| 20 | +plugin. |
| 21 | + |
| 22 | +## Use-case |
| 23 | + |
| 24 | +Test your service locally, without having to deploy it first. |
| 25 | + |
| 26 | +## Setup |
| 27 | + |
| 28 | +```bash |
| 29 | +npm install |
| 30 | +serverless dynamodb install (or to use a persistent docker dynamodb instead, open a new terminal: cd ./dynamodb && docker-compose up -d) |
| 31 | +serverless offline start |
| 32 | +serverless dynamodb migrate (this imports schema) |
| 33 | +``` |
| 34 | + |
| 35 | +## Run service offline |
| 36 | + |
| 37 | +```bash |
| 38 | +serverless offline start |
| 39 | +``` |
| 40 | + |
| 41 | +## Usage |
| 42 | + |
| 43 | +You can create, retrieve, update, or delete todos with the following commands: |
| 44 | + |
| 45 | +### Create a Todo |
| 46 | + |
| 47 | +```bash |
| 48 | +curl -X POST -H "Content-Type:application/json" http://localhost:3000/todos --data '{ "text": "Learn Serverless" }' |
| 49 | +``` |
| 50 | + |
| 51 | +Example Result: |
| 52 | +```bash |
| 53 | +{"text":"Learn Serverless","id":"ee6490d0-aa11e6-9ede-afdfa051af86","createdAt":1479138570824,"checked":false,"updatedAt":1479138570824}% |
| 54 | +``` |
| 55 | + |
| 56 | +### List all Todos |
| 57 | + |
| 58 | +```bash |
| 59 | +curl -H "Content-Type:application/json" http://localhost:3000/todos |
| 60 | +``` |
| 61 | + |
| 62 | +Example output: |
| 63 | +```bash |
| 64 | +[{"text":"Deploy my first service","id":"ac90feaa11e6-9ede-afdfa051af86","checked":true,"updatedAt":1479139961304},{"text":"Learn Serverless","id":"206793aa11e6-9ede-afdfa051af86","createdAt":1479139943241,"checked":false,"updatedAt":1479139943241}]% |
| 65 | +``` |
| 66 | + |
| 67 | +### Get one Todo |
| 68 | + |
| 69 | +```bash |
| 70 | +# Replace the <id> part with a real id from your todos table |
| 71 | +curl -H "Content-Type:application/json" http://localhost:3000/todos/<id> |
| 72 | +``` |
| 73 | + |
| 74 | +Example Result: |
| 75 | +```bash |
| 76 | +{"text":"Learn Serverless","id":"ee6490d0-aa11e6-9ede-afdfa051af86","createdAt":1479138570824,"checked":false,"updatedAt":1479138570824}% |
| 77 | +``` |
| 78 | + |
| 79 | +### Update a Todo |
| 80 | + |
| 81 | +```bash |
| 82 | +# Replace the <id> part with a real id from your todos table |
| 83 | +curl -X PUT -H "Content-Type:application/json" http://localhost:3000/todos/<id> --data '{ "text": "Learn Serverless", "checked": true }' |
| 84 | +``` |
| 85 | + |
| 86 | +Example Result: |
| 87 | +```bash |
| 88 | +{"text":"Learn Serverless","id":"ee6490d0-aa11e6-9ede-afdfa051af86","createdAt":1479138570824,"checked":true,"updatedAt":1479138570824}% |
| 89 | +``` |
| 90 | + |
| 91 | +### Delete a Todo |
| 92 | + |
| 93 | +```bash |
| 94 | +# Replace the <id> part with a real id from your todos table |
| 95 | +curl -X DELETE -H "Content-Type:application/json" http://localhost:3000/todos/<id> |
| 96 | +``` |
| 97 | + |
| 98 | +No output |
0 commit comments