connect-rest
Advanced tools
Comparing version 0.0.11 to 0.0.12
@@ -6,3 +6,3 @@ /* | ||
*/ | ||
var VERSION = '0.0.11'; | ||
var VERSION = '0.0.12'; | ||
@@ -146,2 +146,3 @@ var connect = require('connect'); | ||
exports.rester = function( options ) { | ||
var domain; | ||
if( options ){ | ||
@@ -158,2 +159,4 @@ if( options.discoverPath ) | ||
domain = options.domain; | ||
logger.info('connect-rest has been configured. ', options); | ||
@@ -163,2 +166,19 @@ } | ||
return function(req, res, next) { | ||
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) { | ||
console.error('Error sending 500', er, req.url); | ||
domain.dispose(); | ||
} | ||
}); | ||
} | ||
if(!req.query) req.query = {}; | ||
@@ -165,0 +185,0 @@ |
@@ -71,4 +71,3 @@ var PARAMETER_M_DELIMETER = ':'; | ||
else if( this.isObject ){ | ||
return (this.isSubReged ? this.path.path.test( pathname ) : this.path.path == '*' || (this.path.path.toUpperCase()==pathname.toUpperCase()) ) | ||
&& | ||
return (this.isSubReged ? this.path.path.test( pathname ) : this.path.path == '*' || (this.path.path.toUpperCase()==pathname.toUpperCase()) ) && | ||
matchesVersion( semver, version, this.path.version ); | ||
@@ -75,0 +74,0 @@ } |
@@ -31,4 +31,3 @@ var Path = require('./path'); | ||
var found = _.map( | ||
_.filter( this.paths, function( path ){ return path.matchings( version, _, semver); } ) | ||
, function(path){ | ||
_.filter( this.paths, function( path ){ return path.matchings( version, _, semver); } ), function(path){ | ||
return path.path; | ||
@@ -35,0 +34,0 @@ } |
{ | ||
"name": "connect-rest", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "RESTful web services middleware for Connect.", | ||
@@ -44,4 +44,4 @@ "keywords": [ | ||
"readme": "README.md", | ||
"_id": "connect-rest@0.0.11", | ||
"_from": "connect-rest@>=0.0.11" | ||
"_id": "connect-rest@0.0.12", | ||
"_from": "connect-rest@>=0.0.12" | ||
} |
@@ -1,2 +0,2 @@ | ||
[connect-rest](https://github.com/imrefazekas/connect-rest) is a middleware for [connect](http://www.senchalabs.org/connect/) for building REST APIs providing service discovery and path-based parameter mapping and "reflective" publishing as well. | ||
[connect-rest](https://github.com/imrefazekas/connect-rest) is a middleware for [connect](http://www.senchalabs.org/connect/) for building REST APIs providing service discovery and path-based parameter mapping and "reflective" publishing and node domains as well. | ||
@@ -27,2 +27,3 @@ # Usage | ||
- [Reflective publishing](#reflective-publishing) | ||
- [Domain support](#domain-support) | ||
@@ -266,2 +267,21 @@ ## Assign | ||
## Domain support | ||
connect-rest adds support for domain-based error handling. To the options object you can pass a domain too: | ||
var createDomain = require('domain').create; | ||
... | ||
var superDomain = createDomain(); | ||
... | ||
var restDomain = createDomain(); | ||
superDomain.add( restDomain ); | ||
var options = { | ||
apiKeys: [ '849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' ], | ||
discoverPath: 'discover', | ||
protoPath: 'proto', | ||
logger: 'connect-rest', | ||
domain: restDomain | ||
}; | ||
By passing the restDomain object, 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. | ||
## Server - extracted from the tests | ||
@@ -327,2 +347,3 @@ | ||
- 0.0.12 : Domain (introduced in Node 0.8.0) support added | ||
- 0.0.11 : First request parameter now has a callback for async rest calls | ||
@@ -329,0 +350,0 @@ - 0.0.10 : Prototyping added |
@@ -80,3 +80,3 @@ var options = { | ||
var voptions = _.clone( options ); | ||
voptions.path = '/api/twist'; | ||
voptions.path = '/api/twist?api_key=849b7648-14b8-4154-9ef2-8d1dc4c2b7e9'; | ||
voptions.method = 'POST'; | ||
@@ -83,0 +83,0 @@ voptions.headers['accept-version'] = '2.2.0'; |
@@ -30,3 +30,4 @@ function buildUpRestAPI( rest, _ ){ | ||
console.log( 'Received:' + JSON.stringify( request ) + ' ' + JSON.stringify(content) ); | ||
return JSON.stringify(content); | ||
throw new Error('Shake error...'); | ||
//return JSON.stringify(content); | ||
}, {'title': 'Alice in Wonderland'} ); | ||
@@ -33,0 +34,0 @@ } |
@@ -12,37 +12,65 @@ var rest = require('../lib/connect-rest'); | ||
var connectApp = connect(); | ||
global.server = connectApp; | ||
var createDomain = require('domain').create; | ||
connectApp.use( connect.query() ); | ||
var SERVICE_METHOD_PATTERN = /^[a-zA-Z]([a-zA-Z]|\d|_)*$/g; | ||
var serverDomain = createDomain(); | ||
serverDomain.run(function() { | ||
var connectApp = connect(); | ||
global.server = connectApp; | ||
var options = { | ||
'apiKeys': [ '849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' ], | ||
'discoverPath': 'discover', | ||
'protoPath': 'proto', | ||
'logger': 'connect-rest' | ||
}; | ||
connectApp.use( rest.rester( options ) ); | ||
connectApp.use( function(req, res, next){ | ||
serverDomain.add(req); | ||
serverDomain.add(res); | ||
serverDomain.on('error', function(er) { | ||
console.error('Error', er, req.url); | ||
try { | ||
res.writeHead(500); | ||
res.end('Error occurred, sorry.'); | ||
res.on('close', function() { | ||
serverDomain.dispose(); | ||
}); | ||
} catch (er) { | ||
console.error('Error sending 500', er, req.url); | ||
serverDomain.dispose(); | ||
} | ||
}); | ||
next(); | ||
} ); | ||
connectApp.use( connect.query() ); | ||
var server = http.createServer( connectApp ); | ||
var SERVICE_METHOD_PATTERN = /^[a-zA-Z]([a-zA-Z]|\d|_)*$/g; | ||
server.listen( 8080 ); | ||
var restDomain = createDomain(); | ||
serverDomain.add(restDomain); | ||
var options = { | ||
apiKeys: [ '849b7648-14b8-4154-9ef2-8d1dc4c2b7e9' ], | ||
discoverPath: 'discover', | ||
protoPath: 'proto', | ||
logger: 'connect-rest' | ||
,domain: restDomain | ||
}; | ||
connectApp.use( rest.rester( options ) ); | ||
restBuilder.buildUpRestAPI( rest, _ ); | ||
var server = http.createServer( connectApp ); | ||
async.parallel([ | ||
async.apply( caller.testCall1, http, _ ), | ||
async.apply( caller.testCall2, http, _ ), | ||
async.apply( caller.testCall3a, http, _ ), | ||
async.apply( caller.testCall3b, http, _ ), | ||
async.apply( caller.testCall4, http, _ ), | ||
async.apply( caller.testCall5, http, _ ), | ||
async.apply( caller.testCall6, http, _ ), | ||
async.apply( caller.testCall7, http, _ ), | ||
async.apply( caller.testCall8, http, _ ) | ||
], function(err, results){ | ||
console.log('Tests finished.'); | ||
server.close(); | ||
assert.ifError( err ); | ||
server.listen( 8080 ); | ||
restBuilder.buildUpRestAPI( rest, _ ); | ||
async.parallel([ | ||
//async.apply( caller.testCall1, http, _ ), | ||
//async.apply( caller.testCall2, http, _ ), | ||
//async.apply( caller.testCall3a, http, _ ), | ||
//async.apply( caller.testCall3b, http, _ ), | ||
//async.apply( caller.testCall4, http, _ ), | ||
//async.apply( caller.testCall5, http, _ ), | ||
async.apply( caller.testCall6, http, _ ) | ||
//async.apply( caller.testCall7, http, _ ), | ||
//async.apply( caller.testCall8, http, _ ) | ||
], function(err, results){ | ||
console.log('Tests finished.'); | ||
server.close(); | ||
assert.ifError( err ); | ||
}); | ||
}); | ||
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
36977
481
355