diff --git a/app.js b/app.js deleted file mode 100644 index 62f4269..0000000 --- a/app.js +++ /dev/null @@ -1,12 +0,0 @@ -const express = require('express'); -const Mongo = require('./mongo'); - -const app = express(); -const port = 3000; - - -app.get('/test', (request, response) => { - const mongoClient = new Mongo(); -}) - -app.listen(port, () => console.log('Mock is listening at port ', port, '\n')); \ No newline at end of file diff --git a/package.json b/package.json index 0d2548f..320b743 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "", "main": "index.js", "scripts": { - "start": "nodemon ./app.js localhost 3000", + "start": "nodemon ./src/js/app.js localhost 3000", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], diff --git a/src/js/app.js b/src/js/app.js new file mode 100644 index 0000000..a89f2fc --- /dev/null +++ b/src/js/app.js @@ -0,0 +1,19 @@ +const express = require('express'); + +const MongoClient = require('./mongoClient'); + +const app = express(); +const port = 3000; + +const mongoClient = new MongoClient(); + +app.get('/test', (request, response) => { + mongoClient.insert('test', {creationDate: new Date()}, () => { + mongoClient.find('test', {}, results => { + console.log(results); + response.send(JSON.stringify(results)); + }); + }); +}); + +app.listen(port, () => console.log('Mock is listening at port ', port, '\n')); \ No newline at end of file diff --git a/mongo.js b/src/js/mongoClient.js similarity index 81% rename from mongo.js rename to src/js/mongoClient.js index d38cf18..084e29f 100644 --- a/mongo.js +++ b/src/js/mongoClient.js @@ -8,7 +8,7 @@ const mongoConfig = { database: 'express-test' }; -class Mongo { +class MongoClient { constructor() { mongodb.MongoClient.connect(mongoConfig.url, (err, client) => { assert.equal(null, err); @@ -19,16 +19,18 @@ class Mongo { results.forEach(console.log); }); - client.close(); + // client.close(); }); } find(collectionName, query, callback) { - this.db.collection(collectionName).find(query, (error, result) => { - assert.equal(null, error, `Unable to find ${collectionName} entities: ${error}.`); - console.log(`Entities ${collectionName} founded.`); - callback(result); - }); + this.db.collection(collectionName).find(query).toArray() + .then(results => { + console.log(`Entities ${collectionName} founded.`); + console.log(results); + callback(results); + }) + .catch(error => console.error(error)); } insert(collectionName, entity, callback) { @@ -56,4 +58,4 @@ class Mongo { } } -module.exports = Mongo; \ No newline at end of file +module.exports = MongoClient; \ No newline at end of file