-
Notifications
You must be signed in to change notification settings - Fork 113
/
server.js
37 lines (30 loc) · 956 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict';
const jayson = require('jayson');
const jsonParser = require('body-parser').json;
const express = require('express');
const app = express();
const server = new jayson.server({
getHeaders: function(args, context, callback) {
callback(null, context.headers);
},
// old method not receiving a context object (here for reference)
oldMethod: new jayson.Method(function(args, callback) {
callback(null, {});
}, {
// this setting overrides the server option set below for this particular method
useContext: false
})
}, {
// all methods will receive a context object as the second arg
useContext: true
});
app.use(jsonParser());
app.use(function(req, res, next) {
// prepare a context object passed into the JSON-RPC method
const context = {headers: req.headers};
server.call(req.body, context, function(err, result) {
if(err) return next(err);
res.send(result || {});
});
});
app.listen(3001);