-
Notifications
You must be signed in to change notification settings - Fork 510
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
tomasvanrijsse
wants to merge
2
commits into
HackYourFuture:main
Choose a base branch
from
tomasvanrijsse:Week3-QA-content
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Week 3 QA content #462
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
package-lock.json | ||
secrets.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
"mongoConnection": "", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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) | ||
}) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.