Skip to content

Commit

Permalink
Removed dependency of services on express app
Browse files Browse the repository at this point in the history
  • Loading branch information
rixo committed Mar 1, 2016
1 parent 5f6d662 commit 14dcd41
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 57 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,19 @@ var dyson = require('dyson'),
path = require('path');
var options = {
configDir: path.join(__dirname, 'services'),
// server options
port: 8765
// config loader options
configDir: path.join(__dirname, 'services')
};
var configs = dyson.getConfigurations(options);
var appBefore = dyson.createServer(options);
var appAfter = dyson.registerServices(appBefore, options, configs);
var services = dyson.services(options, configs);
var app = dyson.createServer(serverOptions);
app.use(services);
console.log('Dyson listening at port', options.port);
```
Expand All @@ -278,7 +284,7 @@ var options = {
var myApp = express();
var configs = dyson.getConfigurations(options);
dyson.registerServices(myApp, options, configs);
myApp.use(dyson.services(options, configs));
myApp.listen(8765);
```

Expand Down
36 changes: 18 additions & 18 deletions lib/dyson.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ var bootstrap = function(options) {

var app = createServer(options);

return registerServices(app, options, configs);
app.use(services(options, configs));

return app;
};

var getConfigurations = function(options) {
Expand All @@ -55,23 +56,23 @@ var createServer = function(options) {

// Register middleware to Express as service for each config (as in: `app.get(config.path, config.callback);`)

var registerServices = function(app, options, configs) {
var services = function(options, configs) {

var request = express();
request.use(rawBody());
request.use(favicon(__dirname + '/favicon.ico'));
var router = express.Router();
router.use(rawBody());
router.use(favicon(__dirname + '/favicon.ico'));
if(options.bodyParserJsonLimit) {
request.use(bodyParser.json({limit: options.bodyParserJsonLimit}));
router.use(bodyParser.json({limit: options.bodyParserJsonLimit}));
} else {
request.use(bodyParser.json());
router.use(bodyParser.json());
}
if(options.bodyParserUrlencodedLimit) {
request.use(bodyParser.urlencoded({limit: options.bodyParserUrlencodedLimit, extended: true}));
router.use(bodyParser.urlencoded({limit: options.bodyParserUrlencodedLimit, extended: true}));
} else {
request.use(bodyParser.urlencoded({extended: true}));
router.use(bodyParser.urlencoded({extended: true}));
}
request.use(cookieParser());
request.use(cors({origin: true, credentials: true}));
router.use(cookieParser());
router.use(cors({origin: true, credentials: true}));

for(var method in configs) {

Expand All @@ -83,40 +84,39 @@ var registerServices = function(app, options, configs) {
} else {

var middlewares = [
request,
requireParameter.bind(null, config.requireParameters),
config.callback,
delay.middleware.bind(null, config.delay),
config.render
];

util.log('Registering', method.toUpperCase(), 'service at', config.path);
app[method].apply(app, [config.path].concat(middlewares));
app.options(config.path, cors({origin: true, credentials: true}));
router[method].apply(router, [config.path].concat(middlewares));
router.options(config.path, cors({origin: true, credentials: true}));
}

});
}

if(options.proxy) {

app.all('*', delay.middleware.bind(null, options.proxyDelay), proxy.middleware);
router.all('*', delay.middleware.bind(null, options.proxyDelay), proxy.middleware);

} else {

!util.isTest() && app.all('*', function(req, res, next) {
!util.isTest() && router.all('*', function(req, res, next) {
util.error('404 NOT FOUND:', req.url);
res.writeHead(404);
res.end();
});
}

return app;
return router;
};

module.exports = {
bootstrap: bootstrap,
getConfigurations: getConfigurations,
createServer: createServer,
registerServices: registerServices
services: services
};
6 changes: 3 additions & 3 deletions test/dyson.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ describe('dyson', function() {

before(function() {

app = dyson.createServer(options);

configs = {
'get': [
{
Expand Down Expand Up @@ -80,7 +78,9 @@ describe('dyson', function() {

defaults.assign(configs.get, 'get');

dyson.registerServices(app, options, configs);
app = dyson
.createServer(options)
.use(dyson.services(options, configs));

});

Expand Down
79 changes: 47 additions & 32 deletions test/dyson.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,42 @@ var dyson = require('../lib/dyson'),

describe('dyson', function() {

var app = express(),
options = {};
var options = {};

describe('.registerServices', function() {
describe('.services', function() {

var expressRouter;

var app;

before(function() {
dyson.registerServices(app, options, {
get: [
{
path: '/',
callback: function() {},
render: function() {}
}
]
});

expressRouter = express.Router;

express.Router = function() {
return app;
}

});

after(function() {

express.Router = expressRouter;

});

it('should add GET route to Express', function() {
beforeEach(function() {

app = {
get: sinon.spy(),
post: sinon.spy(),
use: sinon.spy(),
options: sinon.spy()
};

});

var spy = sinon.spy(app, 'get');
it('should add GET route to Router', function() {

var config = {
get: [
Expand All @@ -36,20 +52,16 @@ describe('dyson', function() {
]
};

dyson.registerServices(app, options, config);

spy.callCount.should.equal(1);
spy.firstCall.args[0].should.equal(config.get[0].path);
spy.firstCall.args.should.containEql(config.get[0].callback);
spy.firstCall.args.should.containEql(config.get[0].render);
dyson.services(options, config);

app.get.restore();
app.get.callCount.should.equal(1);
app.get.firstCall.args[0].should.equal(config.get[0].path);
app.get.firstCall.args.should.containEql(config.get[0].callback);
app.get.firstCall.args.should.containEql(config.get[0].render);

});

it('should add POST route to Express', function() {

var spy = sinon.spy(app, 'post');
it('should add POST route to Router', function() {

var config = {
post: [
Expand All @@ -61,20 +73,22 @@ describe('dyson', function() {
]
};

dyson.registerServices(app, options, config);
dyson.services(options, config);

spy.callCount.should.equal(1);
spy.firstCall.args[0].should.equal(config.post[0].path);
spy.firstCall.args.should.containEql(config.post[0].callback);
spy.firstCall.args.should.containEql(config.post[0].render);

app.post.restore();
app.post.callCount.should.equal(1);
app.post.firstCall.args[0].should.equal(config.post[0].path);
app.post.firstCall.args.should.containEql(config.post[0].callback);
app.post.firstCall.args.should.containEql(config.post[0].render);

});
});

describe('routes', function() {

var app;

var express = require('express');

before(function() {

var render = function(req, res) {
Expand Down Expand Up @@ -114,7 +128,8 @@ describe('dyson', function() {
]
};

dyson.registerServices(app, options, configs);
app = express()
.use(dyson.services(options, configs));

});

Expand Down

0 comments on commit 14dcd41

Please sign in to comment.