Permalink
Browse files

fixed several bugs and it properly handles GET-based requests now

  • Loading branch information...
BrianBelhumeur committed Jan 9, 2013
1 parent ce419e5 commit e516799a58d24d17dba718f385d955230736e6c8
Showing 331 changed files with 44,377 additions and 211 deletions.
View
@@ -1,4 +1,4 @@
-Copyright (c) 2011 Brian Belhumeur [Brian's last name]@gmail.com
+Copyright (c) 2012 Brian Belhumeur [Brian's last name]@gmail.com
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
View
75 README
@@ -1,65 +1,66 @@
-This is a JSON-RPC v2 handler middleware for the Connect / Express libraries on node.js <http://nodejs.org>.
+This is a JSON-RPC v2 handler middleware for the Express libraries on node.js <http://nodejs.org>.
First, simply include jsonRPC2 in your configure/use statements before app.router:
app.configure(function(){
- ...
- app.use( express.jsonRPC2() );
- ...
- app.use( app.router );
+ ...
+ app.use( express.jsonRPC2() );
+ ...
+ app.use( app.router );
});
Then wihin a route, use res.rpc to handle a given method. Unless the request is a notification, a response
function will be passed in after the parameters to handle the response.
app.post('/path/for/rpc/calls', function (req, res, next) {
- // notification (no response expected)
- res.rpc('notification_method', function (params) {
- // do processing here
- });
+ // notification (no response expected)
+ res.rpc('notification_method', function (params) {
+ // do processing here
+ });
- // non-notification (response expected)
- res.rpc('method_name', function (params, respond) {
- // do processing here
+ // non-notification (response expected)
+ res.rpc('method_name', function (params, respond) {
+ // do processing here
- // if everything is OK return result object:
- respond( { result: resultData } );
+ // if everything is OK return result object:
+ respond( { result: resultData } );
- // if something is wrong, return an error object:
- respond({
- error: {
- code: 'INVALID_PARAMS', // or use numeric code
- // optional:
- message: 'You gave me invalid parameters!',
- data: data
- }
- });
- });
+ // if something is wrong, return an error code OR:
+ respond(jsonrpc.INVALID_PARAMS)
+ // extended error object
+ respond({
+ error: {
+ code: jsonrpc.INVALID_PARAMS,
+ message: 'You gave me invalid parameters!',
+ data: data
+ }
+ });
+ });
});
The middleware provides standard error codes and messages defined by the JSON-RPC version 2 spec at:
http://www.jsonrpc.org/spec.html
You can reference error codes by static string. For example, if you return:
{
- error: {
- code: 'INVALID_REQUEST'
- }
+ error: {
+ code: 'INVALID_REQUEST'
+ }
}
it will be converted to:
{
- error: {
- code: -32600,
- message: 'Invalid request'
- }
+ error: {
+ code: -32600,
+ message: 'Invalid request'
+ }
}
The error code constants for reference:
-Static string Code Message
+Static string Code Message
------------------------------------------------
-'PARSE_ERROR' -32700 'Parse error'
-'INVALID_REQUEST' -32600 'Invalid request'
-'METHOD_NOT_FOUND' -32601 'Method not found'
-'INVALID_PARAMS' -32602 'Invalid parameters'
-'INTERNAL_ERROR' -32603 'Internal error'
+'PARSE_ERROR' -32700 'Parse error'
+'INVALID_REQUEST' -32600 'Invalid request'
+'METHOD_NOT_FOUND' -32601 'Method not found'
+'INVALID_PARAMS' -32602 'Invalid parameters'
+'INTERNAL_ERROR' -32603 'Internal error'
View
@@ -1 +1 @@
-module.exports = require( './lib/jsonrpc2' );
+module.exports = require( './lib/jsonrpc' );
Oops, something went wrong.

0 comments on commit e516799

Please sign in to comment.