From 0a89dc5dbbb320320d29b11df53e06ad6f74fe2d Mon Sep 17 00:00:00 2001 From: Shaun Hamilton <51722130+Sky020@users.noreply.github.com> Date: Fri, 6 Nov 2020 21:38:27 +0000 Subject: [PATCH] feat: remove instructions and hyperdev (#31) * feat: remove instructions and hyperdev * fix: remove mongoose, change declaration, add dotenv * remove header and footer --- .gitignore | 2 + .hyperdev-assets | 51 ----- README.md | 4 +- myApp.js | 264 +++---------------------- package-lock.json | 212 +------------------- package.json | 52 ++--- sample.env | 1 + server.js | 487 +++++++++++++++++++++++++++------------------- views/index.html | 32 +-- 9 files changed, 353 insertions(+), 752 deletions(-) create mode 100644 .gitignore delete mode 100644 .hyperdev-assets create mode 100644 sample.env diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..97aca2e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +node_modules \ No newline at end of file diff --git a/.hyperdev-assets b/.hyperdev-assets deleted file mode 100644 index 7ae2374..0000000 --- a/.hyperdev-assets +++ /dev/null @@ -1,51 +0,0 @@ -{"name":"drag-in-files-copy.svg","date":"2016-03-16T18:27:31.021Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files-copy.svg","type":"image/svg+xml","size":6229,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files-copy.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(100,156,204)","uuid":"tkviVwlaQ2RryM4r"} -{"name":"drag-in-files.svg","date":"2016-03-16T18:21:25.984Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","type":"image/svg+xml","size":7990,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(100,156,204)","uuid":"Pr0LH5dpGcRJ2QSU"} -{"name":"click-for-details.svg","date":"2016-03-16T18:21:23.511Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","type":"image/svg+xml","size":6408,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(244,188,188)","uuid":"nuMndaSEzn2KoNFG"} -{"name":"use-url.svg","date":"2016-03-16T18:21:20.130Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fuse-url.svg","type":"image/svg+xml","size":6155,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fuse-url.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(44,180,188)","uuid":"LiyD5Lxg8FrmZlhz"} -{"name":"click-for-details.svg","date":"2016-03-16T18:20:57.943Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","type":"image/svg+xml","size":6408,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(244,188,188)","uuid":"04MB8gA2GPoSJ7pp"} -{"name":"drag-in-files.svg","date":"2016-03-16T18:20:56.379Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","type":"image/svg+xml","size":7990,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(100,156,204)","uuid":"q8Au1vYvVFpuGd5b"} -{"name":"use-url.svg","date":"2016-03-16T18:20:52.651Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fuse-url.svg","type":"image/svg+xml","size":6155,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fuse-url.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(44,180,188)","uuid":"9FiKvjvvr4C3oG0i"} -{"name":"1.svg","date":"2016-03-16T18:18:11.204Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2F1.svg","type":"image/svg+xml","size":20524,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2F1.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(100,156,204)","uuid":"MR02bReHwXODf2iH"} -{"name":"2.svg","date":"2016-03-16T18:18:09.776Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2F2.svg","type":"image/svg+xml","size":6597,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2F2.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(244,188,188)","uuid":"Q5gjW3biIe5ubDSw"} -{"name":"3.svg","date":"2016-03-16T18:18:08.145Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2F3.svg","type":"image/svg+xml","size":6856,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2F3.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(44,180,188)","uuid":"ZtH2QDmOOe11lCXh"} -{"name":"drag-in-files.svg","date":"2016-03-16T18:15:33.737Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","type":"image/svg+xml","size":7990,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(100,156,204)","uuid":"5ZhCFJBZeljgrOsm"} -{"name":"click-for-details.svg","date":"2016-03-16T18:15:31.732Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","type":"image/svg+xml","size":6408,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(244,188,188)","uuid":"j9DYGSrBdfoh5Qgf"} -{"name":"drag-in-files.svg","date":"2016-03-16T18:13:11.062Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","type":"image/svg+xml","size":7990,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(100,156,204)","uuid":"xs11lHNV40rcZiOr"} -{"name":"click-for-details.svg","date":"2016-03-16T18:13:07.869Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","type":"image/svg+xml","size":6397,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(244,188,188)","uuid":"oDrtrMLQDzHj3fCi"} -{"name":"drag-in-files.svg","date":"2016-03-16T18:12:09.577Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","type":"image/svg+xml","size":7990,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(100,156,204)","uuid":"QZQEhdAJwQlN15xh"} -{"name":"click-for-details.svg","date":"2016-03-16T18:12:06.958Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","type":"image/svg+xml","size":6395,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(244,188,188)","uuid":"4DsIsyOkpGM5vhnm"} -{"name":"use-url.svg","date":"2016-03-16T18:12:02.187Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fuse-url.svg","type":"image/svg+xml","size":6155,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fuse-url.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(44,180,188)","uuid":"2D0DVLCP7wUxzaXm"} -{"name":"click-for-details.svg","date":"2016-03-16T18:11:49.564Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","type":"image/svg+xml","size":6395,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(244,188,188)","uuid":"ellCYXezSjscGSrr"} -{"name":"drag-in-files.svg","date":"2016-03-16T18:11:44.256Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","type":"image/svg+xml","size":7990,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(100,156,204)","uuid":"96WPtGmfDbGjk1Cq"} -{"name":"drag-in-files.svg","date":"2016-03-16T18:11:25.392Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","type":"image/svg+xml","size":7990,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(100,156,204)","uuid":"w2iCDrwXU3boaMb0"} -{"uuid":"w2iCDrwXU3boaMb0","deleted":true} -{"uuid":"ellCYXezSjscGSrr","deleted":true} -{"uuid":"96WPtGmfDbGjk1Cq","deleted":true} -{"uuid":"4DsIsyOkpGM5vhnm","deleted":true} -{"uuid":"QZQEhdAJwQlN15xh","deleted":true} -{"uuid":"oDrtrMLQDzHj3fCi","deleted":true} -{"uuid":"xs11lHNV40rcZiOr","deleted":true} -{"uuid":"5ZhCFJBZeljgrOsm","deleted":true} -{"uuid":"j9DYGSrBdfoh5Qgf","deleted":true} -{"uuid":"2D0DVLCP7wUxzaXm","deleted":true} -{"uuid":"04MB8gA2GPoSJ7pp","deleted":true} -{"uuid":"q8Au1vYvVFpuGd5b","deleted":true} -{"uuid":"MR02bReHwXODf2iH","deleted":true} -{"uuid":"9FiKvjvvr4C3oG0i","deleted":true} -{"uuid":"ZtH2QDmOOe11lCXh","deleted":true} -{"uuid":"Q5gjW3biIe5ubDSw","deleted":true} -{"uuid":"Pr0LH5dpGcRJ2QSU","deleted":true} -{"uuid":"tkviVwlaQ2RryM4r","deleted":true} -{"uuid":"LiyD5Lxg8FrmZlhz","deleted":true} -{"uuid":"nuMndaSEzn2KoNFG","deleted":true} -{"name":"use-url.svg","date":"2016-03-29T18:22:42.337Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fuse-url.svg","type":"image/svg+xml","size":12848,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fuse-url.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(44,180,188)","uuid":"BPzdWy4z8l1Yn4EQ"} -{"name":"drag-in-files.svg","date":"2016-03-29T18:22:44.084Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","type":"image/svg+xml","size":14823,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(100,156,204)","uuid":"VQVhKMzbgQcBopQk"} -{"uuid":"BPzdWy4z8l1Yn4EQ","deleted":true} -{"uuid":"VQVhKMzbgQcBopQk","deleted":true} -{"name":"click-for-details.svg","date":"2016-03-29T18:22:55.141Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","type":"image/svg+xml","size":12623,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(244,188,188)","uuid":"QTRGE6XDqUiSoYO9"} -{"name":"drag-in-files.svg","date":"2016-03-29T18:22:57.596Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","type":"image/svg+xml","size":14823,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(100,156,204)","uuid":"RS7rnVaX9eGP4Lri"} -{"uuid":"RS7rnVaX9eGP4Lri","deleted":true} -{"uuid":"QTRGE6XDqUiSoYO9","deleted":true} -{"name":"drag-in-files.svg","date":"2016-03-29T18:23:08.746Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","type":"image/svg+xml","size":14823,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fdrag-in-files.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(100,156,204)","uuid":"fxoJRXwNdZeThuNz"} -{"name":"click-for-details.svg","date":"2016-03-29T18:23:10.422Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","type":"image/svg+xml","size":12623,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fclick-for-details.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(244,188,188)","uuid":"Lp2si7Cpft2ktNM4"} -{"name":"use-url.svg","date":"2016-03-29T18:23:12.439Z","url":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fuse-url.svg","type":"image/svg+xml","size":12848,"imageWidth":276,"imageHeight":276,"thumbnail":"https://s3.amazonaws.com/hyperweb-editor-assets/us-east-1%3Ad0d03a8e-22bf-451d-ba15-f08d8f4e99ba%2Fuse-url.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(44,180,188)","uuid":"EjI8Y9wdZ9DEXI17"} diff --git a/README.md b/README.md index a5d6c24..c2e953d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1 @@ -FCC Mongo & Mongoose Challenges -=============================== -[![Run on Repl.it](https://repl.it/badge/github/freeCodeCamp/boilerplate-express)](https://repl.it/github/freeCodeCamp/boilerplate-express) \ No newline at end of file +# [MongoDB and Mongoose Challenges](https://www.freecodecamp.org/learn/apis-and-microservices/mongodb-and-mongoose/) diff --git a/myApp.js b/myApp.js index 90bc218..1ad5eed 100644 --- a/myApp.js +++ b/myApp.js @@ -1,274 +1,60 @@ -/********************************************** -* 3. FCC Mongo & Mongoose Challenges -* ================================== -***********************************************/ - -/** # MONGOOSE SETUP # -/* ================== */ - -/** 1) Install & Set up mongoose */ - -// Add mongodb and mongoose to the project's package.json. Then require -// mongoose. Store your Mongo Atlas database URI in the private .env file -// as MONGO_URI. Connect to the database using the following syntax: -// -// mongoose.connect(, { useNewUrlParser: true, useUnifiedTopology: true }); +require('dotenv').config(); +let Person; -/** # SCHEMAS and MODELS # -/* ====================== */ - -/** 2) Create a 'Person' Model */ - -// First of all we need a **Schema**. Each schema maps to a MongoDB collection -// and defines the shape of the documents within that collection. Schemas are -// building block for Models. They can be nested to create complex models, -// but in this case we'll keep things simple. A model allows you to create -// instances of your objects, called **documents**. - -// Create a person having this prototype : - -// - Person Prototype - -// -------------------- -// name : string [required] -// age : number -// favoriteFoods : array of strings (*) - -// Use the mongoose basic *schema types*. If you want you can also add more -// fields, use simple validators like `required` or `unique`, and set -// `default` values. See the [mongoose docs](http://mongoosejs.com/docs/guide.html). - -// - -var Person /* = */ - -// **Note**: Glitch is a real server, and in real servers interactions with -// the db are placed in handler functions, to be called when some event happens -// (e.g. someone hits an endpoint on your API). We'll follow the same approach -// in these exercises. The `done()` function is a callback that tells us that -// we can proceed after completing an asynchronous operation such as inserting, -// searching, updating or deleting. It's following the Node convention and -// should be called as `done(null, data)` on success, or `done(err)` on error. -// **Warning** - When interacting with remote services, **errors may occur** ! - -// - Example - -// var someFunc = function(done) { -// ... do something (risky) ... -// if(error) return done(error); -// done(null, result); -// }; - -/** # [C]RUD part I - CREATE # -/* ========================== */ - -/** 3) Create and Save a Person */ - -// Create a `document` instance using the `Person` constructor you build before. -// Pass to the constructor an object having the fields `name`, `age`, -// and `favoriteFoods`. Their types must be conformant to the ones in -// the Person `Schema`. Then call the method `document.save()` on the returned -// document instance, passing to it a callback using the Node convention. -// This is a common pattern, all the **CRUD** methods take a callback -// function like this as the last argument. - -// - Example - -// ... -// person.save(function(err, data) { -// ...do your stuff here... -// }); - -var createAndSavePerson = function(done) { - +const createAndSavePerson = (done) => { done(null /*, data*/); - }; -/** 4) Create many People with `Model.create()` */ - -// Sometimes you need to create many Instances of your Models, -// e.g. when seeding a database with initial data. `Model.create()` -// takes an array of objects like [{name: 'John', ...}, {...}, ...], -// as the 1st argument, and saves them all in the db. -// Modify the createManyPeople function to create many people using -// Model.create() with the argument arrayOfPeople. -// Note: You can reuse the model you instantiated in the previous exercise. - -var createManyPeople = function(arrayOfPeople, done) { - - done(null/*, data*/); - +const createManyPeople = (arrayOfPeople, done) => { + done(null /*, data*/); }; -/** # C[R]UD part II - READ # -/* ========================= */ - -/** 5) Use `Model.find()` */ - -// Find all the people having a given name, using `Model.find() -> [Person]` -// In its simplest usage, `Model.find()` accepts a **query** document (a JSON -// object ) as the first argument, and returns an **array** of matches. -// It supports an extremely wide range of search options. Check it in the docs. -// Use the function argument `personName` as search key. - -var findPeopleByName = function(personName, done) { - - done(null/*, data*/); - +const findPeopleByName = (personName, done) => { + done(null /*, data*/); }; -/** 6) Use `Model.findOne()` */ - -// `Model.findOne()` behaves like `.find()`, but it returns **only one** -// document, even if there are more. It is especially useful -// when searching by properties that you have declared as unique. -// Find just one person which has a certain food in her favorites, -// using `Model.findOne() -> Person`. Use the function -// argument `food` as search key - -var findOneByFood = function(food, done) { - - done(null/*, data*/); - +const findOneByFood = (food, done) => { + done(null /*, data*/); }; -/** 7) Use `Model.findById()` */ - -// When saving a document, mongodb automatically add the field `_id`, -// and set it to a unique alphanumeric key. Searching by `_id` is an -// extremely frequent operation, so `moongose` provides a dedicated -// method for it. Find the (only!!) person having a certain Id, -// using `Model.findById() -> Person`. -// Use the function argument 'personId' as search key. - -var findPersonById = function(personId, done) { - - done(null/*, data*/); - +const findPersonById = (personId, done) => { + done(null /*, data*/); }; -/** # CR[U]D part III - UPDATE # -/* ============================ */ +const findEditThenSave = (personId, done) => { + const foodToAdd = "hamburger"; -/** 8) Classic Update : Find, Edit then Save */ - -// In the good old days this was what you needed to do if you wanted to edit -// a document and be able to use it somehow e.g. sending it back in a server -// response. Mongoose has a dedicated updating method : `Model.update()`, -// which is directly binded to the low-level mongo driver. -// It can bulk edit many documents matching certain criteria, but it doesn't -// pass the edited document to its callback, only a 'status' message. -// Furthermore it makes validation difficult, because it just -// direcly calls the mongodb driver. - -// Find a person by Id ( use any of the above methods ) with the parameter -// `personId` as search key. Add "hamburger" to the list of her `favoriteFoods` -// (you can use Array.push()). Then - **inside the find callback** - `.save()` -// the updated `Person`. - -// [*] Hint: This may be tricky if in your `Schema` you declared -// `favoriteFoods` as an `Array` without specifying the type (i.e. `[String]`). -// In that case `favoriteFoods` defaults to `Mixed` type, and you have to -// manually mark it as edited using `document.markModified('edited-field')` -// (http://mongoosejs.com/docs/schematypes.html - #Mixed ) - -var findEditThenSave = function(personId, done) { - var foodToAdd = 'hamburger'; - - done(null/*, data*/); + done(null /*, data*/); }; -/** 9) New Update : Use `findOneAndUpdate()` */ +const findAndUpdate = (personName, done) => { + const ageToSet = 20; -// Recent versions of `mongoose` have methods to simplify documents updating. -// Some more advanced features (i.e. pre/post hooks, validation) beahve -// differently with this approach, so the 'Classic' method is still useful in -// many situations. `findByIdAndUpdate()` can be used when searching by Id. -// -// Find a person by `name` and set her age to `20`. Use the function parameter -// `personName` as search key. -// -// Hint: We want you to return the **updated** document. In order to do that -// you need to pass the options document `{ new: true }` as the 3rd argument -// to `findOneAndUpdate()`. By default the method -// passes the unmodified object to its callback. - -var findAndUpdate = function(personName, done) { - var ageToSet = 20; - - done(null/*, data*/); + done(null /*, data*/); }; -/** # CRU[D] part IV - DELETE # -/* =========================== */ - -/** 10) Delete one Person */ - -// Delete one person by her `_id`. You should use one of the methods -// `findByIdAndRemove()` or `findOneAndRemove()`. They are similar to the -// previous update methods. They pass the removed document to the cb. -// As usual, use the function argument `personId` as search key. - -var removeById = function(personId, done) { - - done(null/*, data*/); - +const removeById = (personId, done) => { + done(null /*, data*/); }; -/** 11) Delete many People */ +const removeManyPeople = (done) => { + const nameToRemove = "Mary"; -// `Model.remove()` is useful to delete all the documents matching given criteria. -// Delete all the people whose name is "Mary", using `Model.remove()`. -// Pass to it a query ducument with the "name" field set, and of course a callback. -// -// Note: `Model.remove()` doesn't return the removed document, but a document -// containing the outcome of the operation, and the number of items affected. -// Don't forget to pass it to the `done()` callback, since we use it in tests. - -var removeManyPeople = function(done) { - var nameToRemove = "Mary"; - - done(null/*, data*/); + done(null /*, data*/); }; -/** # C[R]UD part V - More about Queries # -/* ======================================= */ +const queryChain = (done) => { + const foodToSearch = "burrito"; -/** 12) Chain Query helpers */ - -// If you don't pass the `callback` as the last argument to `Model.find()` -// (or to the other similar search methods introduced before), the query is -// not executed, and can even be stored in a variable for later use. -// This kind of object enables you to build up a query using chaining syntax. -// The actual db search is executed when you finally chain -// the method `.exec()`, passing your callback to it. -// There are many query helpers, here we'll use the most 'famous' ones. - -// Find people who like "burrito". Sort them alphabetically by name, -// Limit the results to two documents, and hide their age. -// Chain `.find()`, `.sort()`, `.limit()`, `.select()`, and then `.exec()`, -// passing the `done(err, data)` callback to it. - -var queryChain = function(done) { - var foodToSearch = "burrito"; - - done(null/*, data*/); + done(null /*, data*/); }; /** **Well Done !!** /* You completed these challenges, let's go celebrate ! */ -/** # Further Readings... # -/* ======================= */ -// If you are eager to learn and want to go deeper, You may look at : -// * Indexes ( very important for query efficiency ), -// * Pre/Post hooks, -// * Validation, -// * Schema Virtuals and Model, Static, and Instance methods, -// * and much more in the [mongoose docs](http://mongoosejs.com/docs/) - - //----- **DO NOT EDIT BELOW THIS LINE** ---------------------------------- exports.PersonModel = Person; diff --git a/package-lock.json b/package-lock.json index 4cc9a64..5de5933 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,20 +18,6 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, - "bl": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/bl/-/bl-2.2.0.tgz", - "integrity": "sha512-wbgvOpqopSr7uq6fJrLH8EsvYMJf9gzfo2jCsL2eTy75qXPukA4pCgHamOQkZtY5vmfVtjB+P3LNlMHW5CEZXA==", - "requires": { - "readable-stream": "^2.3.5", - "safe-buffer": "^5.1.1" - } - }, - "bluebird": { - "version": "3.5.1", - "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz", - "integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA==" - }, "body-parser": { "version": "1.19.0", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz", @@ -64,11 +50,6 @@ } } }, - "bson": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/bson/-/bson-1.1.4.tgz", - "integrity": "sha512-S/yKGU1syOMzO86+dGpg2qGoDL0zvzcb262G+gqEy6TgP6rt6z6qxSFX/8X6vLC91P7G7C3nLs0+bvDzmvBA3Q==" - }, "bytes": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", @@ -97,31 +78,6 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" }, - "core-util-is": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", - "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" - }, - "debug": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", - "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", - "requires": { - "ms": "2.0.0" - }, - "dependencies": { - "ms": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", - "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" - } - } - }, - "denque": { - "version": "1.4.1", - "resolved": "https://registry.npmjs.org/denque/-/denque-1.4.1.tgz", - "integrity": "sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ==" - }, "depd": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", @@ -132,6 +88,11 @@ "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" }, + "dotenv": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", + "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==" + }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -270,37 +231,16 @@ "safer-buffer": ">= 2.1.2 < 3" } }, - "inherits": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" - }, "ipaddr.js": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==" }, - "isarray": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", - "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" - }, - "kareem": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.1.tgz", - "integrity": "sha512-l3hLhffs9zqoDe8zjmb/mAN4B8VT3L56EUvKNqLFVs9YlFA+zx7ke1DO8STAdDyYNkeSo1nKmjuvQeI12So8Xw==" - }, "media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" }, - "memory-pager": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", - "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", - "optional": true - }, "merge-descriptors": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", @@ -329,64 +269,6 @@ "mime-db": "1.44.0" } }, - "mongodb": { - "version": "3.5.9", - "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.5.9.tgz", - "integrity": "sha512-vXHBY1CsGYcEPoVWhwgxIBeWqP3dSu9RuRDsoLRPTITrcrgm1f0Ubu1xqF9ozMwv53agmEiZm0YGo+7WL3Nbug==", - "requires": { - "bl": "^2.2.0", - "bson": "^1.1.4", - "denque": "^1.4.1", - "require_optional": "^1.0.1", - "safe-buffer": "^5.1.2", - "saslprep": "^1.0.0" - } - }, - "mongoose": { - "version": "5.9.20", - "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.9.20.tgz", - "integrity": "sha512-vRP6Csu2obzSl3ed7kTQMrolBNgweiRJ/eBU1PSe/rJfjqWS1oqDE2D1ZPGxkVOsKXs7Gyd84GAXerj8IB2UWg==", - "requires": { - "bson": "^1.1.4", - "kareem": "2.3.1", - "mongodb": "3.5.9", - "mongoose-legacy-pluralize": "1.0.2", - "mpath": "0.7.0", - "mquery": "3.2.2", - "ms": "2.1.2", - "regexp-clone": "1.0.0", - "safe-buffer": "5.1.2", - "sift": "7.0.1", - "sliced": "1.0.1" - } - }, - "mongoose-legacy-pluralize": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz", - "integrity": "sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ==" - }, - "mpath": { - "version": "0.7.0", - "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.7.0.tgz", - "integrity": "sha512-Aiq04hILxhz1L+f7sjGyn7IxYzWm1zLNNXcfhDtx04kZ2Gk7uvFdgZ8ts1cWa/6d0TQmag2yR8zSGZUmp0tFNg==" - }, - "mquery": { - "version": "3.2.2", - "resolved": "https://registry.npmjs.org/mquery/-/mquery-3.2.2.tgz", - "integrity": "sha512-XB52992COp0KP230I3qloVUbkLUxJIu328HBP2t2EsxSFtf4W1HPSOBWOXf1bqxK4Xbb66lfMJ+Bpfd9/yZE1Q==", - "requires": { - "bluebird": "3.5.1", - "debug": "3.1.0", - "regexp-clone": "^1.0.0", - "safe-buffer": "5.1.2", - "sliced": "1.0.1" - } - }, - "ms": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" - }, "negotiator": { "version": "0.6.2", "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", @@ -410,11 +292,6 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, - "process-nextick-args": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", - "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" - }, "proxy-addr": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz", @@ -445,39 +322,6 @@ "unpipe": "1.0.0" } }, - "readable-stream": { - "version": "2.3.7", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz", - "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==", - "requires": { - "core-util-is": "~1.0.0", - "inherits": "~2.0.3", - "isarray": "~1.0.0", - "process-nextick-args": "~2.0.0", - "safe-buffer": "~5.1.1", - "string_decoder": "~1.1.1", - "util-deprecate": "~1.0.1" - } - }, - "regexp-clone": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/regexp-clone/-/regexp-clone-1.0.0.tgz", - "integrity": "sha512-TuAasHQNamyyJ2hb97IuBEif4qBHGjPHBS64sZwytpLEqtBQ1gPJTnOaQ6qmpET16cK14kkjbazl6+p0RRv0yw==" - }, - "require_optional": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz", - "integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==", - "requires": { - "resolve-from": "^2.0.0", - "semver": "^5.1.0" - } - }, - "resolve-from": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz", - "integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c=" - }, "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", @@ -488,20 +332,6 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, - "saslprep": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz", - "integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==", - "optional": true, - "requires": { - "sparse-bitfield": "^3.0.3" - } - }, - "semver": { - "version": "5.7.1", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", - "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==" - }, "send": { "version": "0.17.1", "resolved": "https://registry.npmjs.org/send/-/send-0.17.1.tgz", @@ -560,38 +390,11 @@ "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==" }, - "sift": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/sift/-/sift-7.0.1.tgz", - "integrity": "sha512-oqD7PMJ+uO6jV9EQCl0LrRw1OwsiPsiFQR5AR30heR+4Dl7jBBbDLnNvWiak20tzZlSE1H7RB30SX/1j/YYT7g==" - }, - "sliced": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/sliced/-/sliced-1.0.1.tgz", - "integrity": "sha1-CzpmK10Ewxd7GSa+qCsD+Dei70E=" - }, - "sparse-bitfield": { - "version": "3.0.3", - "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", - "integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=", - "optional": true, - "requires": { - "memory-pager": "^1.0.2" - } - }, "statuses": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" }, - "string_decoder": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", - "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", - "requires": { - "safe-buffer": "~5.1.0" - } - }, "toidentifier": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", @@ -611,11 +414,6 @@ "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" }, - "util-deprecate": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", - "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" - }, "utils-merge": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", diff --git a/package.json b/package.json index 9a1b3d4..225fabd 100644 --- a/package.json +++ b/package.json @@ -1,27 +1,27 @@ { - "name": "fcc-mongo-mongoose-challenges", - "version": "0.0.1", - "description": "A boilerplate project", - "main": "server.js", - "scripts": { - "start": "node server.js" - }, - "dependencies": { - "body-parser": "^1.15.2", - "express": "^4.12.4", - "mongoose": "^5.9.20" - }, - "engines": { - "node": "4.4.5" - }, - "repository": { - "type": "git", - "url": "https://hyperdev.com/#!/project/welcome-project" - }, - "keywords": [ - "node", - "hyperdev", - "express" - ], - "license": "MIT" -} + "name": "fcc-mongo-mongoose-challenges", + "version": "0.0.1", + "description": "A boilerplate project", + "main": "server.js", + "scripts": { + "start": "node server.js" + }, + "dependencies": { + "body-parser": "^1.15.2", + "dotenv": "^8.2.0", + "express": "^4.12.4" + }, + "engines": { + "node": "12.*" + }, + "repository": { + "type": "git", + "url": "https://github.com/freeCodeCamp/boilerplate-mongomongoose.git" + }, + "keywords": [ + "node", + "mongoose", + "express" + ], + "license": "MIT" +} \ No newline at end of file diff --git a/sample.env b/sample.env new file mode 100644 index 0000000..0439b02 --- /dev/null +++ b/sample.env @@ -0,0 +1 @@ +MONGO_URI= \ No newline at end of file diff --git a/server.js b/server.js index 35e84d2..9750b71 100644 --- a/server.js +++ b/server.js @@ -3,28 +3,30 @@ * the verification process may break *******************************************/ -var express = require('express'); -var app = express(); -try{ - var mongoose = require('mongoose'); +const express = require("express"); +const app = express(); +let mongoose; +try { + mongoose = require("mongoose"); } catch (e) { console.log(e); } -var fs = require('fs'); -var path = require('path'); -var bodyParser = require('body-parser'); -var router = express.Router(); +const fs = require("fs"); +const path = require("path"); +const bodyParser = require("body-parser"); +const router = express.Router(); -var enableCORS = function(req, res, next) { +const enableCORS = function (req, res, next) { if (!process.env.DISABLE_XORIGIN) { - var allowedOrigins = ['https://marsh-glazer.gomix.me','https://narrow-plane.gomix.me', 'https://www.freecodecamp.com']; - var origin = req.headers.origin; - if(!process.env.XORIGIN_RESTRICT || allowedOrigins.indexOf(origin) > -1) { + const allowedOrigins = ["https://www.freecodecamp.org"]; + const origin = req.headers.origin; + if (!process.env.XORIGIN_RESTRICT || allowedOrigins.indexOf(origin) > -1) { console.log(req.method); res.set({ - "Access-Control-Allow-Origin" : origin, - "Access-Control-Allow-Methods" : "GET, POST, OPTIONS", - "Access-Control-Allow-Headers" : "Origin, X-Requested-With, Content-Type, Accept" + "Access-Control-Allow-Origin": origin, + "Access-Control-Allow-Methods": "GET, POST, OPTIONS", + "Access-Control-Allow-Headers": + "Origin, X-Requested-With, Content-Type, Accept", }); } } @@ -33,100 +35,124 @@ var enableCORS = function(req, res, next) { // global setting for safety timeouts to handle possible // wrong callbacks that will never be called -var timeout = 10000; +const TIMEOUT = 10000; -app.use(bodyParser.urlencoded({extended: 'false'})); +app.use(bodyParser.urlencoded({ extended: "false" })); app.use(bodyParser.json()); -app.get('/', function(req, res) { - res.sendFile(path.join(__dirname, 'views', 'index.html')); +app.get("/", function (req, res) { + res.sendFile(path.join(__dirname, "views", "index.html")); }); -router.get('/file/*?', function(req, res, next) { - if(req.params[0] === '.env') { return next({status: 401, message: 'ACCESS DENIED'}) } - fs.readFile(path.join(__dirname, req.params[0]), function(err, data){ - if(err) { return next(err) } - res.type('txt').send(data.toString()); +router.get("/file/*?", function (req, res, next) { + if (req.params[0] === ".env") { + return next({ status: 401, message: "ACCESS DENIED" }); + } + fs.readFile(path.join(__dirname, req.params[0]), function (err, data) { + if (err) { + return next(err); + } + res.type("txt").send(data.toString()); }); }); -router.get('/is-mongoose-ok', function(req, res) { +router.get("/is-mongoose-ok", function (req, res) { if (mongoose) { - res.json({isMongooseOk: !!mongoose.connection.readyState}) + res.json({ isMongooseOk: !!mongoose.connection.readyState }); } else { - res.json({isMongooseOk: false}) + res.json({ isMongooseOk: false }); } }); -var Person = require('./myApp.js').PersonModel; +const Person = require("./myApp.js").PersonModel; -router.use(function(req, res, next) { - if(req.method !== 'OPTIONS' && Person.modelName !== 'Person') { - return next({message: 'Person Model is not correct'}); +router.use(function (req, res, next) { + if (req.method !== "OPTIONS" && Person.modelName !== "Person") { + return next({ message: "Person Model is not correct" }); } next(); }); -router.post('/mongoose-model', function(req, res, next) { +router.post("/mongoose-model", function (req, res, next) { // try to create a new instance based on their model // verify it's correctly defined in some way - var p; + let p; p = new Person(req.body); res.json(p); }); -var createPerson = require('./myApp.js').createAndSavePerson; -router.get('/create-and-save-person', function(req, res, next) { +const createPerson = require("./myApp.js").createAndSavePerson; +router.get("/create-and-save-person", function (req, res, next) { // in case of incorrect function use wait timeout then respond - var t = setTimeout(() => { next({message: 'timeout'}) }, timeout); - createPerson(function(err, data) { + let t = setTimeout(() => { + next({ message: "timeout" }); + }, TIMEOUT); + createPerson(function (err, data) { clearTimeout(t); - if(err) { return (next(err)); } - if(!data) { - console.log('Missing `done()` argument'); - return next({message: 'Missing callback argument'}); + if (err) { + return next(err); } - Person.findById(data._id, function(err, pers) { - if(err) { return (next(err)); } - res.json(pers); - pers.remove(); - }); - }); -}); - -var createPeople = require('./myApp.js').createManyPeople; -router.post('/create-many-people', function(req, res, next) { - Person.remove({}, function(err) { - if(err) { return (next(err)); } - // in case of incorrect function use wait timeout then respond - var t = setTimeout(() => { next({message: 'timeout'}) }, timeout); - createPeople(req.body, function(err, data) { - clearTimeout(t); - if(err) { return (next(err)); } - if(!data) { - console.log('Missing `done()` argument'); - return next({message: 'Missing callback argument'}); + if (!data) { + console.log("Missing `done()` argument"); + return next({ message: "Missing callback argument" }); + } + Person.findById(data._id, function (err, pers) { + if (err) { + return next(err); } - Person.find({}, function(err, pers){ - if(err) { return (next(err)); } - res.json(pers); - Person.remove().exec(); - }); + res.json(pers); + pers.remove(); }); }); }); -var findByName = require('./myApp.js').findPeopleByName; -router.post('/find-all-by-name', function(req, res, next) { - var t = setTimeout(() => { next({message: 'timeout'}) }, timeout); - Person.create(req.body, function(err, pers) { - if(err) { return next(err) } - findByName(pers.name, function(err, data) { +const createPeople = require("./myApp.js").createManyPeople; +router.post("/create-many-people", function (req, res, next) { + Person.remove({}, function (err) { + if (err) { + return next(err); + } + // in case of incorrect function use wait timeout then respond + let t = setTimeout(() => { + next({ message: "timeout" }); + }, TIMEOUT); + createPeople(req.body, function (err, data) { clearTimeout(t); - if(err) { return next(err) } - if(!data) { - console.log('Missing `done()` argument'); - return next({message: 'Missing callback argument'}); + if (err) { + return next(err); + } + if (!data) { + console.log("Missing `done()` argument"); + return next({ message: "Missing callback argument" }); + } + Person.find({}, function (err, pers) { + if (err) { + return next(err); + } + res.json(pers); + Person.remove().exec(); + }); + }); + }); +}); + +const findByName = require("./myApp.js").findPeopleByName; +router.post("/find-all-by-name", function (req, res, next) { + let t = setTimeout(() => { + next({ message: "timeout" }); + }, TIMEOUT); + Person.create(req.body, function (err, pers) { + if (err) { + return next(err); + } + findByName(pers.name, function (err, data) { + clearTimeout(t); + if (err) { + return next(err); + } + if (!data) { + console.log("Missing `done()` argument"); + return next({ message: "Missing callback argument" }); } res.json(data); Person.remove().exec(); @@ -134,81 +160,75 @@ router.post('/find-all-by-name', function(req, res, next) { }); }); -var findByFood = require('./myApp.js').findOneByFood; -router.post('/find-one-by-food', function(req, res, next) { - var t = setTimeout(() => { next({message: 'timeout'}) }, timeout); - var p = new Person(req.body); - p.save(function(err, pers) { - if(err) { return next(err) } - findByFood(pers.favoriteFoods[0], function(err, data) { - clearTimeout(t); - if(err) { return next(err) } - if(!data) { - console.log('Missing `done()` argument'); - return next({message: 'Missing callback argument'}); - } - res.json(data); - p.remove(); - }); - }); -}); - -var findById = require('./myApp.js').findPersonById; -router.get('/find-by-id', function(req, res, next) { - var t = setTimeout(() => { next({message: 'timeout'}) }, timeout); - var p = new Person({name: 'test', age: 0, favoriteFoods: ['none']}); - p.save(function(err, pers) { - if(err) { return next(err) } - findById(pers._id, function(err, data) { - clearTimeout(t); - if(err) { return next(err) } - if(!data) { - console.log('Missing `done()` argument'); - return next({message: 'Missing callback argument'}); - } - res.json(data); - p.remove(); - }); - }); -}); - -var findEdit = require('./myApp.js').findEditThenSave; -router.post('/find-edit-save', function(req, res, next) { - var t = setTimeout(() => { next({message: 'timeout'}) }, timeout); - var p = new Person(req.body); - p.save(function(err, pers) { - if(err) { return next(err) } - try { - findEdit(pers._id, function(err, data) { - clearTimeout(t); - if(err) { return next(err) } - if(!data) { - console.log('Missing `done()` argument'); - return next({message: 'Missing callback argument'}); - } - res.json(data); - p.remove(); - }); - } catch (e) { - console.log(e); - return next(e); +const findByFood = require("./myApp.js").findOneByFood; +router.post("/find-one-by-food", function (req, res, next) { + let t = setTimeout(() => { + next({ message: "timeout" }); + }, TIMEOUT); + let p = new Person(req.body); + p.save(function (err, pers) { + if (err) { + return next(err); } + findByFood(pers.favoriteFoods[0], function (err, data) { + clearTimeout(t); + if (err) { + return next(err); + } + if (!data) { + console.log("Missing `done()` argument"); + return next({ message: "Missing callback argument" }); + } + res.json(data); + p.remove(); + }); }); }); -var update = require('./myApp.js').findAndUpdate; -router.post('/find-one-update', function(req, res, next) { - var t = setTimeout(() => { next({message: 'timeout'}) }, timeout); - var p = new Person(req.body); - p.save(function(err, pers) { - if(err) { return next(err) } +const findById = require("./myApp.js").findPersonById; +router.get("/find-by-id", function (req, res, next) { + let t = setTimeout(() => { + next({ message: "timeout" }); + }, TIMEOUT); + let p = new Person({ name: "test", age: 0, favoriteFoods: ["none"] }); + p.save(function (err, pers) { + if (err) { + return next(err); + } + findById(pers._id, function (err, data) { + clearTimeout(t); + if (err) { + return next(err); + } + if (!data) { + console.log("Missing `done()` argument"); + return next({ message: "Missing callback argument" }); + } + res.json(data); + p.remove(); + }); + }); +}); + +const findEdit = require("./myApp.js").findEditThenSave; +router.post("/find-edit-save", function (req, res, next) { + let t = setTimeout(() => { + next({ message: "timeout" }); + }, TIMEOUT); + let p = new Person(req.body); + p.save(function (err, pers) { + if (err) { + return next(err); + } try { - update(pers.name, function(err, data) { + findEdit(pers._id, function (err, data) { clearTimeout(t); - if(err) { return next(err) } + if (err) { + return next(err); + } if (!data) { - console.log('Missing `done()` argument'); - return next({ message: 'Missing callback argument' }); + console.log("Missing `done()` argument"); + return next({ message: "Missing callback argument" }); } res.json(data); p.remove(); @@ -220,30 +240,70 @@ router.post('/find-one-update', function(req, res, next) { }); }); -var removeOne = require('./myApp.js').removeById; -router.post('/remove-one-person', function(req, res, next) { - Person.remove({}, function(err) { - if(err) { return next(err) } - var t = setTimeout(() => { next({message: 'timeout'}) }, timeout); - var p = new Person(req.body); - p.save(function(err, pers) { - if(err) { return next(err) } +const update = require("./myApp.js").findAndUpdate; +router.post("/find-one-update", function (req, res, next) { + let t = setTimeout(() => { + next({ message: "timeout" }); + }, TIMEOUT); + let p = new Person(req.body); + p.save(function (err, pers) { + if (err) { + return next(err); + } + try { + update(pers.name, function (err, data) { + clearTimeout(t); + if (err) { + return next(err); + } + if (!data) { + console.log("Missing `done()` argument"); + return next({ message: "Missing callback argument" }); + } + res.json(data); + p.remove(); + }); + } catch (e) { + console.log(e); + return next(e); + } + }); +}); + +const removeOne = require("./myApp.js").removeById; +router.post("/remove-one-person", function (req, res, next) { + Person.remove({}, function (err) { + if (err) { + return next(err); + } + let t = setTimeout(() => { + next({ message: "timeout" }); + }, TIMEOUT); + let p = new Person(req.body); + p.save(function (err, pers) { + if (err) { + return next(err); + } try { - removeOne(pers._id, function(err, data) { + removeOne(pers._id, function (err, data) { clearTimeout(t); - if(err) { return next(err) } - if(!data) { - console.log('Missing `done()` argument'); - return next({message: 'Missing callback argument'}); + if (err) { + return next(err); } - console.log(data) - Person.count(function(err, cnt) { - if(err) { return next(err) } + if (!data) { + console.log("Missing `done()` argument"); + return next({ message: "Missing callback argument" }); + } + console.log(data); + Person.count(function (err, cnt) { + if (err) { + return next(err); + } data = data.toObject(); data.count = cnt; - console.log(data) + console.log(data); res.json(data); - }) + }); }); } catch (e) { console.log(e); @@ -253,26 +313,36 @@ router.post('/remove-one-person', function(req, res, next) { }); }); -var removeMany = require('./myApp.js').removeManyPeople; -router.post('/remove-many-people', function(req, res, next) { - Person.remove({}, function(err) { - if(err) { return next(err) } - var t = setTimeout(() => { next({message: 'timeout'}) }, timeout); - Person.create(req.body, function(err, pers) { - if(err) { return next(err) } +const removeMany = require("./myApp.js").removeManyPeople; +router.post("/remove-many-people", function (req, res, next) { + Person.remove({}, function (err) { + if (err) { + return next(err); + } + let t = setTimeout(() => { + next({ message: "timeout" }); + }, TIMEOUT); + Person.create(req.body, function (err, pers) { + if (err) { + return next(err); + } try { - removeMany(function(err, data) { + removeMany(function (err, data) { clearTimeout(t); - if(err) { return next(err) } - if(!data) { - console.log('Missing `done()` argument'); - return next({message: 'Missing callback argument'}); + if (err) { + return next(err); } - Person.count(function(err, cnt) { - if(err) { return next(err) } + if (!data) { + console.log("Missing `done()` argument"); + return next({ message: "Missing callback argument" }); + } + Person.count(function (err, cnt) { + if (err) { + return next(err); + } if (data.ok === undefined) { // for mongoose v4 - try { + try { data = JSON.parse(data); } catch (e) { console.log(e); @@ -282,32 +352,40 @@ router.post('/remove-many-people', function(req, res, next) { res.json({ n: data.n, count: cnt, - ok: data.ok + ok: data.ok, }); - }) + }); }); } catch (e) { console.log(e); return next(e); } }); - }) + }); }); -var chain = require('./myApp.js').queryChain; -router.post('/query-tools', function(req, res, next) { - var t = setTimeout(() => { next({message: 'timeout'}) }, timeout); - Person.remove({}, function(err) { - if(err) { return next(err) } - Person.create(req.body, function(err, pers) { - if(err) { return next(err) } +const chain = require("./myApp.js").queryChain; +router.post("/query-tools", function (req, res, next) { + let t = setTimeout(() => { + next({ message: "timeout" }); + }, TIMEOUT); + Person.remove({}, function (err) { + if (err) { + return next(err); + } + Person.create(req.body, function (err, pers) { + if (err) { + return next(err); + } try { - chain(function(err, data) { + chain(function (err, data) { clearTimeout(t); - if(err) { return next(err) } + if (err) { + return next(err); + } if (!data) { - console.log('Missing `done()` argument'); - return next({ message: 'Missing callback argument' }); + console.log("Missing `done()` argument"); + return next({ message: "Missing callback argument" }); } res.json(data); }); @@ -316,31 +394,32 @@ router.post('/query-tools', function(req, res, next) { return next(e); } }); - }) + }); }); -app.use('/_api', enableCORS, router); +app.use("/_api", enableCORS, router); // Error handler -app.use(function(err, req, res, next) { - if(err) { - res.status(err.status || 500) - .type('txt') - .send(err.message || 'SERVER ERROR'); +app.use(function (err, req, res, next) { + if (err) { + res + .status(err.status || 500) + .type("txt") + .send(err.message || "SERVER ERROR"); } }); // Unmatched routes handler -app.use(function(req, res){ - if(req.method.toLowerCase() === 'options') { +app.use(function (req, res) { + if (req.method.toLowerCase() === "options") { res.end(); } else { - res.status(404).type('txt').send('Not Found'); + res.status(404).type("txt").send("Not Found"); } -}) +}); -var listener = app.listen(process.env.PORT || 3000 , function () { - console.log('Your app is listening on port ' + listener.address().port); +const listener = app.listen(process.env.PORT || 3000, function () { + console.log("Your app is listening on port " + listener.address().port); }); /******************************************** diff --git a/views/index.html b/views/index.html index 694fcf1..c9a744c 100644 --- a/views/index.html +++ b/views/index.html @@ -10,12 +10,15 @@ - Welcome to HyperDev! - - - - - + MongoDB & Mongoose | freeCodeCamp.org + + + +