Skip to content

Commit

Permalink
Merge branch 'update_to_mongo_v3'
Browse files Browse the repository at this point in the history
  • Loading branch information
sgnn7 committed Mar 23, 2018
2 parents 7035cac + db76e3e commit 1deb540
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
11 changes: 5 additions & 6 deletions chapter_3/prototype_service/application_server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ const COLLECTION_NAME = 'words';
const SERVER_PORT = 8000;

const app = express();
const client = mongo.MongoClient();
const dbUri = `mongodb://${DB_HOST}/${DB_NAME}`;
const words = [];

app.set('view engine', 'pug')
app.use(bodyParser.urlencoded({ extended: false }))

function loadWordsFromDatabase() {
return client.connect(dbUri).then((db) => {
return db.collection(COLLECTION_NAME).find({}).toArray();
return mongo.connect(dbUri).then(client => {
return client.db(DB_NAME).collection(COLLECTION_NAME).find({}).toArray();
})
.then((docs) => {
words.push.apply(words, docs.map(doc => doc.word));
Expand All @@ -36,9 +35,9 @@ app.post('/new', (req, res) => {

console.info(`Got word: ${word}`);
if (word) {
client.connect(dbUri).then((db) => {
db.collection(COLLECTION_NAME).insertOne({ word }, () => {
db.close();
mongo.connect(dbUri).then(client => {
client.db(DB_NAME).collection(COLLECTION_NAME).insertOne({ word }, () => {
client.close();
words.push(word);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.4",
"mongodb": "^2.2.31",
"mongodb": "^3.0.4",
"pug": "^2.0.0-rc.3"
}
}
11 changes: 5 additions & 6 deletions chapter_4/clustered_application/application_server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ const COLLECTION_NAME = 'words';
const SERVER_PORT = 8000;

const app = express();
const client = mongo.MongoClient();
const dbUri = `mongodb://${DB_HOST}/${DB_NAME}`;

app.set('view engine', 'pug')
app.use(bodyParser.urlencoded({ extended: false }))

function loadWordsFromDatabase() {
return client.connect(dbUri).then((db) => {
return db.collection(COLLECTION_NAME).find({}).toArray();
return mongo.connect(dbUri).then(client => {
return client.db(DB_NAME).collection(COLLECTION_NAME).find({}).toArray();
})
.then((docs) => {
return docs.map(doc => doc.word);
Expand All @@ -38,9 +37,9 @@ app.post('/new', (req, res) => {

console.info(`Got word: ${word}`);
if (word) {
client.connect(dbUri).then((db) => {
db.collection(COLLECTION_NAME).insertOne({ word }, () => {
db.close();
mongo.connect(dbUri).then(client => {
client.db(DB_NAME).collection(COLLECTION_NAME).insertOne({ word }, () => {
client.close();
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.4",
"mongodb": "^2.2.31",
"mongodb": "^3.0.4",
"pug": "^2.0.0-rc.3"
}
}

0 comments on commit 1deb540

Please sign in to comment.