Last active
July 13, 2017 08:16
-
-
Save kaworu/57f9f28585302b09d251063ff9721a3d to your computer and use it in GitHub Desktop.
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
diff --git a/day3/app.js b/day3/app.js | |
index 637f47f..a9a5700 100644 | |
--- a/day3/app.js | |
+++ b/day3/app.js | |
@@ -6,6 +6,7 @@ | |
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const mongoose = require("mongoose"); | |
+const MongoClient = require("mongodb").MongoClient; | |
/* our applications modules */ | |
const Post = require("./models/post"); | |
@@ -19,16 +20,17 @@ app.locals.databases = { | |
development: 'mongodb://localhost/yabe-development', | |
testing: 'mongodb://localhost/yabe-testing', | |
}; | |
-/* mongoose & MongoDB stuff */ | |
-mongoose.Promise = global.Promise; // Use native promises | |
-const is_development = (process.env.NODE_ENV === 'development'); | |
-mongoose.set('debug', is_development); | |
/* mongoose connection */ | |
-app.locals.connect = function () { | |
+app.locals.connect = function (callback) { | |
const env = process.env.NODE_ENV; | |
const database = app.locals.databases[env]; | |
console.log(`NODE_ENV=${env}, connecting to ${database}`); | |
- return mongoose.connect(database, {useMongoClient: true}); | |
+ MongoClient.connect(database, function(err, db) { | |
+ if (err) | |
+ return callback(err); | |
+ app.locals.db = db; | |
+ return callback(); | |
+ }); | |
}; | |
/* setup */ | |
@@ -39,8 +41,10 @@ app.use(bodyParser.urlencoded({extended: false})) | |
app.post("/api/posts", function (req, res, next) { | |
const input = req.body; | |
// app.locals.posts.push(input); | |
- Post.create(input).catch(next).then(created => { | |
- return res.status(201 /* Created */).send(created); | |
+ app.locals.db.collection('posts').insertOne(input, (err, result) => { | |
+ if (err) | |
+ return next(err); | |
+ return res.status(201 /* Created */).send(result.ops[0]); | |
}); | |
}); | |
diff --git a/day3/index.js b/day3/index.js | |
index c9523c3..754395f 100644 | |
--- a/day3/index.js | |
+++ b/day3/index.js | |
@@ -6,7 +6,7 @@ | |
const app = require("./app"); | |
// launch our application | |
-app.locals.connect().then(() => { | |
+app.locals.connect(err => { | |
console.log(app.locals.name + ' connected to MongoDB'); | |
app.listen(app.locals.port, function () { | |
console.log(app.locals.name + ' listening on port ' + app.locals.port); | |
diff --git a/day3/package.json b/day3/package.json | |
index c9ac612..0345384 100644 | |
--- a/day3/package.json | |
+++ b/day3/package.json | |
@@ -13,6 +13,7 @@ | |
"dependencies": { | |
"body-parser": "^1.17.2", | |
"express": "^4.15.3", | |
+ "mongodb": "^2.2.30", | |
"mongoose": "^4.11.1" | |
}, | |
"devDependencies": { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment