Last active
October 5, 2019 04:59
-
-
Save harrisonmalone/1de3da86fe1fc5598e4be82cf4df8a8c to your computer and use it in GitHub Desktop.
mongodb driver example, shows how mongoose works somewhat
This file contains hidden or 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
| const express = require('express') | |
| const app = express(); | |
| const morgan = require('morgan'); | |
| const ObjectID = require('mongodb').ObjectID; | |
| const MongoClient = require('mongodb').MongoClient; | |
| const uri = "mongodb+srv://harrisonmalone:jHuIZ0tli^&r4quG%5RT3sWMEeoKmp@my-first-cluster-vk1sm.mongodb.net/test?retryWrites=true&w=majority"; | |
| const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); | |
| client.connect(err => { | |
| if (err) { | |
| throw(err) | |
| } | |
| else { | |
| const collection = client.db("sample_mflix").collection("comments"); | |
| app.listen(5000, () => { | |
| console.log('Listening on Port 5000'); | |
| }) | |
| app.use(morgan('dev')) | |
| app.use(express.json()) | |
| app.get('/comments/new', (req, res, next) => { | |
| res.sendFile(__dirname + '/index.html'); | |
| }) | |
| app.get('/comments', (req, res, next) => { | |
| collection.find().limit(10).toArray((err, results) => { | |
| if (err) { res.status(500).send(err)} | |
| res.send(results) | |
| }) | |
| }) | |
| app.get('/comments/:id', (req, res, next) => { | |
| const id = ObjectID(req.params.id) | |
| collection.find(id).toArray((err, result) => { | |
| if(err) { res.status(500).send(err)} | |
| res.send({ | |
| message: 'Successfully Found', | |
| comment: result | |
| }) | |
| }) | |
| }) | |
| app.post('/comments', (req, res, next) => { | |
| const data = req.body | |
| let comment = { | |
| name: data.name, | |
| email: data.email, | |
| movie_id: data.movie_id, | |
| text: data.text, | |
| date: new Date() | |
| } | |
| collection.insertOne(comment, (err, results) => { | |
| if (err) { res.status(500).send(err)} | |
| res.send({ | |
| message: 'Successfully Added Comment', | |
| comment: comment | |
| }) | |
| }) | |
| }) | |
| app.delete('/comments/delete/:id', (req, res, next) => { | |
| let id = ObjectID(req.params.id); | |
| collection.deleteOne({_id: id}, (err, result) => { | |
| if(err) {res.status(500).send(err)} | |
| res.send({ | |
| message: 'Comment Deleted', | |
| comment: result | |
| }) | |
| }) | |
| }); | |
| } | |
| client.close(); | |
| }); | |
This file contains hidden or 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
| { | |
| "name": "cli", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "start": "nodemon app.js" | |
| }, | |
| "keywords": [], | |
| "author": "", | |
| "license": "ISC", | |
| "dependencies": { | |
| "express": "^4.17.1", | |
| "mongodb": "^3.3.2", | |
| "morgan": "^1.9.1", | |
| "nodemon": "^1.19.3" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment