connect-rest
Advanced tools
Comparing version 1.6.0 to 1.6.5
@@ -18,2 +18,4 @@ /* | ||
var domain = require('domain'); | ||
var Route = require('./route'); | ||
@@ -45,3 +47,3 @@ var Bus = require('./bus'); | ||
var Logger = require('./logger'); | ||
var VERSION = exports.VERSION = '1.4.2'; | ||
var VERSION = exports.VERSION = '1.6.5'; | ||
var logger; | ||
@@ -353,4 +355,2 @@ | ||
exports.rester = function( options ) { | ||
var domain; | ||
options = options || {}; | ||
@@ -373,3 +373,3 @@ | ||
domain = options.domain; | ||
var domainOpts = options.domain; | ||
@@ -380,25 +380,3 @@ bus.initBus( options.monitoring, logger.restlog ); | ||
return function restMaker(req, res, next) { | ||
if (!req.query) | ||
req.query = req.url.indexOf('?') ? qs.parse( parseurl(req).query ) : {}; | ||
var pathname = url.parse( req.url ).pathname; | ||
if( domain ){ | ||
domain.add(req); | ||
domain.add(res); | ||
domain.on('error', function(err) { | ||
try { | ||
res.statusCode = 500; | ||
res.end(err.message + '\n'); | ||
res.on('close', function() { | ||
domain.dispose(); | ||
}); | ||
} catch (er) { | ||
logger.restlog( er, '', { pathname: pathname } ); | ||
domain.dispose(); | ||
} | ||
}); | ||
} | ||
function perform( pathname, req, res, next ){ | ||
logger.restlog( null, 'Incoming request.', { headers: req.headers, query: req.query, httpVersion: req.httpVersion, method: req.method, originalUrl: req.originalUrl, pathname: pathname }, 'verbose' ); | ||
@@ -435,2 +413,33 @@ | ||
processRequest(req, res, matching, req.body); | ||
} | ||
return function restMaker(req, res, next) { | ||
if (!req.query) | ||
req.query = req.url.indexOf('?') ? qs.parse( parseurl(req).query ) : {}; | ||
var pathname = url.parse( req.url ).pathname; | ||
if( domainOpts ){ | ||
var reqDomain = domain.create(); | ||
reqDomain.on('error', function (err) { | ||
if( domainOpts.closeWorker ) | ||
domainOpts.closeWorker( req, res ); | ||
if( domainOpts.closeRequest ) | ||
domainOpts.closeRequest( req, res ); | ||
else{ | ||
res.statusCode = 500; | ||
res.setHeader('content-type', 'text/plain'); | ||
res.end('There was a problem!\n'); | ||
} | ||
}); | ||
reqDomain.add(req); | ||
reqDomain.add(res); | ||
reqDomain.run(function() { | ||
perform( pathname, req, res, next ); | ||
}); | ||
} | ||
else | ||
perform( pathname, req, res, next ); | ||
}; | ||
@@ -437,0 +446,0 @@ }; |
{ | ||
"name": "connect-rest", | ||
"version": "1.6.0", | ||
"version": "1.6.5", | ||
"description": "Exceptionally featureful RESTful web services middleware for Connect.", | ||
@@ -57,3 +57,3 @@ "keywords": [ | ||
}, | ||
"_id": "connect-rest@1.6.0" | ||
"_id": "connect-rest@1.6.5" | ||
} |
@@ -697,11 +697,5 @@ CONNECT-REST - Exceptionally featureful Restful web services middleware for connect node.js | ||
## Domain support | ||
[connect-rest](https://github.com/imrefazekas/connect-rest) adds support for domain-based error handling. To the options object you can pass a domain too: | ||
[connect-rest](https://github.com/imrefazekas/connect-rest) adds support for domain-based error handling. To the options object you can pass a boolean value requesting the lib to create domain as [NodeJS docs defines](http://nodejs.org/api/domain.html#domain_domain): | ||
```javascript | ||
var createDomain = require('domain').create; | ||
... | ||
var superDomain = createDomain(); | ||
... | ||
var restDomain = createDomain(); | ||
superDomain.add( restDomain ); | ||
var options = { | ||
@@ -712,8 +706,27 @@ apiKeys: [ '849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' ], | ||
logger: 'connect-rest', | ||
domain: restDomain | ||
domain: true | ||
}; | ||
``` | ||
By passing the restDomain object, [connect-rest](https://github.com/imrefazekas/connect-rest) will assign req and rest object to that domain and in any occurring error, it will be sent to the caller with HTTP status code 500. | ||
or you can have a more sophisticated version by passing a complete object as follows: | ||
```javascript | ||
var options = { | ||
apiKeys: [ '849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' ], | ||
discoverPath: 'discover', | ||
protoPath: 'proto', | ||
logger: 'connect-rest', | ||
domain: { | ||
closeWorker: function(req, res){... }, | ||
closeRequest: function(req, res){... } | ||
} | ||
}; | ||
``` | ||
Where the function __closeWorker__ is an optional function which is called when error occurred and supposed to close the current worker instance if app is running in a node cluster. | ||
The function __closeRequest__ is an optional function which is called to close the request object on error if you want to perform custom response message. By default [connect-rest](https://github.com/imrefazekas/connect-rest) sets the error code 500 and returns a simple error message 'There was a problem!'. | ||
[Back to Feature list](#features) | ||
@@ -720,0 +733,0 @@ |
@@ -34,3 +34,4 @@ var chai = require('chai'), | ||
discoverPath: 'discover', | ||
protoPath: 'proto' | ||
protoPath: 'proto', | ||
domain: true | ||
}; | ||
@@ -37,0 +38,0 @@ app.use( rest.rester( options ) ); |
@@ -35,3 +35,4 @@ var http = require('http'); | ||
protoPath: 'proto', | ||
loose: { after: 1000 } | ||
loose: { after: 1000 }, | ||
domain: true | ||
}; | ||
@@ -38,0 +39,0 @@ app.use( rest.rester( options ) ); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1272081
1412
926