Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week 3 QA content #462

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Week3/todo backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock.json
secrets.js
13 changes: 13 additions & 0 deletions Week3/todo backend/README.md
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 20 additions & 0 deletions Week3/todo backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "todo-backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"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"
}
}
114 changes: 114 additions & 0 deletions Week3/todo backend/postman-collection.json
Original file line number Diff line number Diff line change
@@ -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": []
}
]
}
3 changes: 3 additions & 0 deletions Week3/todo backend/secrets.js.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
"mongoConnection": "",
}
58 changes: 58 additions & 0 deletions Week3/todo backend/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import express from 'express';
import bodyParser from 'body-parser';
const app = express();
import { default as mongodb } from 'mongodb';
const MongoClient = mongodb.MongoClient;
import config from './secrets.js';

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose students are supposed to fill some working code here.


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)
})