diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..7217ea0cd --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "pwa-node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/index.js", + "envFile": "${workspaceFolder}/.env" + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 1651d8d99..96aab03bc 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ This is an example REST Api designed to be deployed on Cyclic.sh [![Deploy to Cyclic](https://deploy.cyclic.app/button.svg)](https://deploy.cyclic.app/) +Remember to add your environment variable 'CYCLIC_DB:name_of_my_custom_dynamodb_Table' to the environments variables, under '[Variables](https://app.cyclic.sh)' Tab in the Cyclic.sh console ## Examples diff --git a/env.sample b/env.sample index fafbde598..b3a099894 100644 --- a/env.sample +++ b/env.sample @@ -1,2 +1,6 @@ -CYCLIC_DB=some-long-app-nameCyclicDB -BUCKET=cyclic-some-long-app-name-id-direction-1 +# fill in the ... below. Go to Data/Storage Tab, on your account webpage https://app.cyclic.sh/#/app/ +CYCLIC_DB=...CyclicDB +AWS_REGION="us-west-1" +AWS_ACCESS_KEY_ID="..." +AWS_SECRET_ACCESS_KEY="..." +AWS_SESSION_TOKEN="..." diff --git a/index.js b/index.js index 60b01d0e5..0ab5e7044 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ const express = require('express') const app = express() -const db = require('cyclic-dynamodb') +const CyclicDb = require('cyclic-dynamodb'); +const db = CyclicDb(process.env.CYCLIC_DB); app.use(express.json()) app.use(express.urlencoded({ extended: true })) @@ -19,6 +20,13 @@ app.use(express.urlencoded({ extended: true })) // app.use(express.static('public', options)) // ############################################################################# + + +app.get('/user/:userId', async (req, res) => { + req.params; + res.json(req.params); +}); + // Create or Update an item app.post('/:col/:key', async (req, res) => { console.log(req.body) @@ -27,7 +35,7 @@ app.post('/:col/:key', async (req, res) => { const key = req.params.key console.log(`from collection: ${col} delete key: ${key} with params ${JSON.stringify(req.params)}`) const item = await db.collection(col).set(key, req.body) - console.log(JSON.stringify(item, null, 2)) + console.log(item) res.json(item).end() }) @@ -37,7 +45,7 @@ app.delete('/:col/:key', async (req, res) => { const key = req.params.key console.log(`from collection: ${col} delete key: ${key} with params ${JSON.stringify(req.params)}`) const item = await db.collection(col).delete(key) - console.log(JSON.stringify(item, null, 2)) + console.log(item) res.json(item).end() }) @@ -45,9 +53,9 @@ app.delete('/:col/:key', async (req, res) => { app.get('/:col/:key', async (req, res) => { const col = req.params.col const key = req.params.key - console.log(`from collection: ${col} get key: ${key} with params ${JSON.stringify(req.params)}`) + console.log(`from collection: ${col} get key: ${key} with params ${JSON.stringify(req.params)}`); const item = await db.collection(col).get(key) - console.log(JSON.stringify(item, null, 2)) + console.log(item) res.json(item).end() }) @@ -61,9 +69,7 @@ app.get('/:col', async (req, res) => { }) // Catch all handler for all other request. -app.use('*', (req, res) => { - res.json({ msg: 'no route handler found' }).end() -}) +app.use(express.static('www')); // Start the server const port = process.env.PORT || 3000 diff --git a/package-lock.json b/package-lock.json index 67d8031f4..00d1a1bfd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,10 +5,11 @@ "requires": true, "packages": { "": { + "name": "starter-rest-api", "version": "1.0.0", "license": "ISC", "dependencies": { - "cyclic-dynamodb": "0.0.21", + "cyclic-dynamodb": "0.0.27", "env-cmd": "^10.1.0", "express": "^4.18.1" }, @@ -1401,9 +1402,9 @@ } }, "node_modules/cyclic-dynamodb": { - "version": "0.0.21", - "resolved": "https://registry.npmjs.org/cyclic-dynamodb/-/cyclic-dynamodb-0.0.21.tgz", - "integrity": "sha512-dVfQj4qICFdSjcOrBtku2gQHA645CSz38nEBFI5JNo8Qb5wuyALpHtyG8Yyh6T3lJAAyQ7CPN39dKp4cgjA8uw==", + "version": "0.0.27", + "resolved": "https://registry.npmjs.org/cyclic-dynamodb/-/cyclic-dynamodb-0.0.27.tgz", + "integrity": "sha512-8AqHqaYVzjWn1S/joQf29oDewVCd1PRGdxzMATe6DcL/AH79TOCDbGR/9Bhh8Ljx6RUDMkXcORNT4Dfm+T/QhA==", "dependencies": { "@aws-sdk/client-dynamodb": "3.82.0", "@aws-sdk/lib-dynamodb": "3.82.0" @@ -4071,9 +4072,9 @@ "dev": true }, "cyclic-dynamodb": { - "version": "0.0.21", - "resolved": "https://registry.npmjs.org/cyclic-dynamodb/-/cyclic-dynamodb-0.0.21.tgz", - "integrity": "sha512-dVfQj4qICFdSjcOrBtku2gQHA645CSz38nEBFI5JNo8Qb5wuyALpHtyG8Yyh6T3lJAAyQ7CPN39dKp4cgjA8uw==", + "version": "0.0.27", + "resolved": "https://registry.npmjs.org/cyclic-dynamodb/-/cyclic-dynamodb-0.0.27.tgz", + "integrity": "sha512-8AqHqaYVzjWn1S/joQf29oDewVCd1PRGdxzMATe6DcL/AH79TOCDbGR/9Bhh8Ljx6RUDMkXcORNT4Dfm+T/QhA==", "requires": { "@aws-sdk/client-dynamodb": "3.82.0", "@aws-sdk/lib-dynamodb": "3.82.0" diff --git a/package.json b/package.json index ebb85f5ea..95ec5ba68 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "nodemon": "^2.0.16" }, "dependencies": { - "cyclic-dynamodb": "0.0.21", + "cyclic-dynamodb": "0.0.27", "env-cmd": "^10.1.0", "express": "^4.18.1" } diff --git a/www/index.html b/www/index.html new file mode 100644 index 000000000..ab9205a56 --- /dev/null +++ b/www/index.html @@ -0,0 +1,27 @@ + +

Starter REST Api

+ +This is an example REST Api designed to be deployed on Cyclic.sh + +Deploy to Cyclic + +

Examples

+ +

Create/Update - Insert/Upsert

+ +curl -i https://localhost:3000/animals/rin%20tin%20tin \ + --data '{"breed":"German Shepard", "gender": "male"}' \ + -XPOST -H 'Content-Type: application/json' + + +

Read All - List

+ +curl -i https://localhost:3000/animals + +

Read

+ +curl -i https://localhost:3000/animals/doggie + +

Delete

+ +curl -i -XDELETE https://localhost:3000/animals/lassy \ No newline at end of file