Skip to content

Commit

Permalink
Merge pull request #11 from apostolidhs/feature/10-smaller-readme-exa…
Browse files Browse the repository at this point in the history
…mple

Feature/10 smaller readme example
  • Loading branch information
apostolidhs committed Jan 30, 2017
2 parents 4ee5ad9 + eef2e1b commit dca609f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 78 deletions.
88 changes: 12 additions & 76 deletions README.md
Expand Up @@ -91,55 +91,23 @@ On the following excerpts we depict a simple NodeJS application in pure NodeJS m
/////////////////////
// ./app.js
var express = require('express');
var mongoose = require('mongoose');
var config = require('./config');
var mongooseConnector = require('./db/mongoose-connector.js');

var db = mongoose.connect(config.MONGODB_URL);
var app = express();
app.get('/', function(req, res){
mongooseConnector.connect(function(db) {
db.collection('myCollection').find().toArray(function(err, items) {
res.send(items);
});
db.collection('myCollection').find().toArray(function(err, items) {
res.send(items);
});
});
app.listen(config.PORT);

/////////////////////
// /db/mongoose-connector.js
var mongoose = require('mongoose');
var logger = require('./logger');
var config = require('./config');

module.exports = {
connect: connect
};

var connected = false;
function connect(cb) {
if (connected) {
cb($mongoose.connection);
return;
}
mongoose.connect(config.MONGODB_URL);]
db.once('open', function() {
logger.log('connected');
connected = true;
cb(mongoose.connection);
});
}

/////////////////////
// ./logger.js
module.exports = {
log: function() { console.log.apply(console, arguments); },
error: function() { console.error.apply(console, arguments); }
};

/////////////////////
// ./config.js
module.exports = {
MONGODB_URL: 'mongodb://localhost:27017/my-db',
POST: 3000
PORT: 3000
};

/////////////////////
Expand All @@ -152,55 +120,23 @@ require('./app');
```javascript
/////////////////////
// /plugins/app/index.js
KlarkModule(module, 'app', function($express, config, dbMongooseConnector) {
KlarkModule(module, 'app', function($express, $mongoose, config) {
var db = $mongoose.connect(config.MONGODB_URL);
var app = $express();
app.get('/', function(req, res){
dbMongooseConnector.connect(function(db) {
db.collection('myCollection').find().toArray(function(err, items) {
res.send(items);
});
db.collection('myCollection').find().toArray(function(err, items) {
res.send(items);
});
});
app.listen(config.PORT);
});

/////////////////////
// /plugins/db/mongoose-connector/index.js
KlarkModule(module, 'dbMongooseConnector', function($mongoose, logger, config) {
var connected = false;
return {
connect: connect
};

function connect(cb) {
if (connected) {
cb($mongoose.connection);
return;
}
$mongoose.connect(config.MONGODB_URL);]
db.once('open', function() {
logger.log('connected');
connected = true;
cb($mongoose.connection);
});
}
});

/////////////////////
// /plugins/logger/index.js
KlarkModule(module, 'logger', function() {
return {
log: function() { console.log.apply(console, arguments); },
error: function() { console.error.apply(console, arguments); }
};
});

/////////////////////
// /plugins/config/index.js
KlarkModule(module, 'config', function() {
return {
MONGODB_URL: 'mongodb://localhost:27017/my-db',
POST: 3000
PORT: 3000
};
});

Expand Down Expand Up @@ -427,9 +363,9 @@ var $$dbMongooseConnector;
var $$_;
var expect;

KlarkModule(module, 'dbMongooseConnectorTest', function($chai, $_, dbMongooseConnector) {
KlarkModule(module, 'dbMongooseConnectorTest', function($chai, _, dbMongooseConnector) {
$$dbMongooseConnector = dbMongooseConnector;
$$_ = $_;
$$_ = _;
expect = $chai.expect;
});

Expand Down
3 changes: 2 additions & 1 deletion lib/configuration-loader.js
Expand Up @@ -28,7 +28,8 @@ function getDefaultConfig() {
base: process.cwd(),
logLevel: 'off',
moduleAlias: {
'_': '$lodash'
'_': '$lodash',
'q': '$q'
}
};
}
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "klark-js",
"version": "1.2.1",
"version": "1.2.2",
"description": "Module loader (plugin system) based on dependency injection for NodeJS applications",
"main": "index",
"scripts": {
Expand Down

0 comments on commit dca609f

Please sign in to comment.