From ae58c917cee77f0ad1d02f657ac61e3fc0beff28 Mon Sep 17 00:00:00 2001 From: Tomas van Rijsse Date: Sun, 21 Feb 2021 20:16:39 +0100 Subject: [PATCH 1/2] mongodb boilerplate restore delete mongo example change the readme --- Week3/todo backend/.gitignore | 2 + Week3/todo backend/README.md | 13 +++ Week3/todo backend/package.json | 19 ++++ Week3/todo backend/postman-collection.json | 114 +++++++++++++++++++++ Week3/todo backend/secrets.json.example | 4 + Week3/todo backend/server.js | 58 +++++++++++ 6 files changed, 210 insertions(+) create mode 100644 Week3/todo backend/.gitignore create mode 100644 Week3/todo backend/README.md create mode 100644 Week3/todo backend/package.json create mode 100644 Week3/todo backend/postman-collection.json create mode 100644 Week3/todo backend/secrets.json.example create mode 100644 Week3/todo backend/server.js diff --git a/Week3/todo backend/.gitignore b/Week3/todo backend/.gitignore new file mode 100644 index 000000000..db556e7f2 --- /dev/null +++ b/Week3/todo backend/.gitignore @@ -0,0 +1,2 @@ +package-lock.json +secrets.json diff --git a/Week3/todo backend/README.md b/Week3/todo backend/README.md new file mode 100644 index 000000000..42b7fc41b --- /dev/null +++ b/Week3/todo backend/README.md @@ -0,0 +1,13 @@ +This is ment for a live Q&A exercise about making a backend for a todo app, with a focus on the native mongodb client. + +On [todomvc.com](http://todomvc.com/) you can see an example todo app, +and this site has an awesome overview of different implementations of the same todo app. + +The `secrets.json.example` should be copied to `secrets.json`. +It's good to mention this practice, to keep passwords out of version control. + +There is `postman-collection.json` file that you can import to easily demonstrate all the endpoints. + +The content of this exercise is based on [a tutorial on Zellwk.com](https://zellwk.com/blog/crud-express-mongodb/) +The `server.js` should be started using `npm run dev` that will use nodemon to hot-reload the server. +There are four CRUD endpoints listed already that can be implemented live. diff --git a/Week3/todo backend/package.json b/Week3/todo backend/package.json new file mode 100644 index 000000000..43bd76d86 --- /dev/null +++ b/Week3/todo backend/package.json @@ -0,0 +1,19 @@ +{ + "name": "todo-backend", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "dev": "nodemon server.js" + }, + "author": "", + "license": "ISC", + "dependencies": { + "body-parser": "^1.19.0", + "express": "^4.17.1", + "mongodb": "^3.6.3" + }, + "devDependencies": { + "nodemon": "^2.0.7" + } +} diff --git a/Week3/todo backend/postman-collection.json b/Week3/todo backend/postman-collection.json new file mode 100644 index 000000000..3bd87bb46 --- /dev/null +++ b/Week3/todo backend/postman-collection.json @@ -0,0 +1,114 @@ +{ + "info": { + "_postman_id": "35e0d81a-cf4f-41d0-b7d8-95905f20ff25", + "name": "ToDo app", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "List", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "localhost:3000/todos", + "host": [ + "localhost" + ], + "port": "3000", + "path": [ + "todos" + ] + } + }, + "response": [] + }, + { + "name": "Create", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "urlencoded", + "urlencoded": [ + { + "key": "title", + "value": "chill out", + "type": "text" + }, + { + "key": "is_checked", + "value": "false", + "type": "text" + } + ] + }, + "url": { + "raw": "localhost:3000/todos", + "host": [ + "localhost" + ], + "port": "3000", + "path": [ + "todos" + ] + } + }, + "response": [] + }, + { + "name": "Update", + "request": { + "method": "PUT", + "header": [], + "body": { + "mode": "urlencoded", + "urlencoded": [ + { + "key": "title", + "value": "evaluate after done", + "type": "text", + "disabled": true + }, + { + "key": "is_checked", + "value": "true", + "type": "text" + } + ] + }, + "url": { + "raw": "localhost:3000/todos/5ffa3c8c10402b00e6af7c57", + "host": [ + "localhost" + ], + "port": "3000", + "path": [ + "todos", + "5ffa3c8c10402b00e6af7c57" + ] + } + }, + "response": [] + }, + { + "name": "Delete", + "request": { + "method": "DELETE", + "header": [], + "url": { + "raw": "localhost:3000/todos/5ffa3b277c5cfb007e758be3", + "host": [ + "localhost" + ], + "port": "3000", + "path": [ + "todos", + "5ffa3b277c5cfb007e758be3" + ] + } + }, + "response": [] + } + ] +} diff --git a/Week3/todo backend/secrets.json.example b/Week3/todo backend/secrets.json.example new file mode 100644 index 000000000..fbcc3e1cf --- /dev/null +++ b/Week3/todo backend/secrets.json.example @@ -0,0 +1,4 @@ +{ + "mongoConnection": "", + "test": "" +} diff --git a/Week3/todo backend/server.js b/Week3/todo backend/server.js new file mode 100644 index 000000000..b005067ae --- /dev/null +++ b/Week3/todo backend/server.js @@ -0,0 +1,58 @@ +const express = require('express'); +const bodyParser = require('body-parser') +const app = express(); +const ObjectID = require('mongodb').ObjectID; +const MongoClient = require('mongodb').MongoClient +const config = require('./secrets.json'); + +async function getMongoCollection() { + const client = new MongoClient(config.mongoConnection); + await client.connect(); + const database = client.db("todo-app"); + return database.collection("todos"); +} + +// Make sure you place body-parser before your CRUD handlers! +app.use(bodyParser.urlencoded({extended: true})) + +app.listen(3000, function () { + console.log('listening on 3000') +}) + +app.get('/todos', async function (req, res) { + + const collection = await getMongoCollection(); + + // http://mongodb.github.io/node-mongodb-native/3.6/tutorials/crud/#read-methods + + res.send('This should list all todos') +}) + +app.post('/todos', async function (req, res) { + + const collection = await getMongoCollection(); + + // http://mongodb.github.io/node-mongodb-native/3.6/tutorials/crud/#insert-documents + + res.send('This should create a single todo'); +}) + +app.put('/todos/:id', async function (req, res) { + + const collection = await getMongoCollection(); + + // http://mongodb.github.io/node-mongodb-native/3.6/tutorials/crud/#updating-documents + // https://stackoverflow.com/questions/4902569/node-js-mongodb-select-document-by-id-node-mongodb-native + + res.send('This should update a single todo: ' + req.params.id) +}) + +app.delete('/todos/:id', async function (req, res) { + + const collection = await getMongoCollection(); + + // http://mongodb.github.io/node-mongodb-native/3.6/tutorials/crud/#removing-documents + + res.send('This should delete a single todo: ' + req.params.id) +}) + From c2dddd82aa207e984aff56966654116766b1e305 Mon Sep 17 00:00:00 2001 From: Tomas van Rijsse Date: Sun, 10 Jan 2021 07:42:18 +0100 Subject: [PATCH 2/2] switch to ES6 syntax --- Week3/todo backend/.gitignore | 2 +- Week3/todo backend/package.json | 1 + .../{secrets.json.example => secrets.js.example} | 3 +-- Week3/todo backend/server.js | 10 +++++----- 4 files changed, 8 insertions(+), 8 deletions(-) rename Week3/todo backend/{secrets.json.example => secrets.js.example} (57%) diff --git a/Week3/todo backend/.gitignore b/Week3/todo backend/.gitignore index db556e7f2..5bb2cad3c 100644 --- a/Week3/todo backend/.gitignore +++ b/Week3/todo backend/.gitignore @@ -1,2 +1,2 @@ package-lock.json -secrets.json +secrets.js diff --git a/Week3/todo backend/package.json b/Week3/todo backend/package.json index 43bd76d86..2ac775034 100644 --- a/Week3/todo backend/package.json +++ b/Week3/todo backend/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "description": "", "main": "index.js", + "type": "module", "scripts": { "dev": "nodemon server.js" }, diff --git a/Week3/todo backend/secrets.json.example b/Week3/todo backend/secrets.js.example similarity index 57% rename from Week3/todo backend/secrets.json.example rename to Week3/todo backend/secrets.js.example index fbcc3e1cf..7086441ba 100644 --- a/Week3/todo backend/secrets.json.example +++ b/Week3/todo backend/secrets.js.example @@ -1,4 +1,3 @@ -{ +export default { "mongoConnection": "", - "test": "" } diff --git a/Week3/todo backend/server.js b/Week3/todo backend/server.js index b005067ae..4d5849160 100644 --- a/Week3/todo backend/server.js +++ b/Week3/todo backend/server.js @@ -1,9 +1,9 @@ -const express = require('express'); -const bodyParser = require('body-parser') +import express from 'express'; +import bodyParser from 'body-parser'; const app = express(); -const ObjectID = require('mongodb').ObjectID; -const MongoClient = require('mongodb').MongoClient -const config = require('./secrets.json'); +import { default as mongodb } from 'mongodb'; +const MongoClient = mongodb.MongoClient; +import config from './secrets.js'; async function getMongoCollection() { const client = new MongoClient(config.mongoConnection);