0.0.4 • Published 10 years ago
conducto v0.0.4
conducto
Node.js framework, REST and realtime
Server
var conducto = require('conducto');
var server = new conducto.Server();
server.use(function(req, res, next)
if (req.method !== 'sum')
return next();
//payload doesn't comply, send an error to the client
if (!req.payload || !Array.isArray(req.payload))
return res('Payload must be an array.');
var sum = 0;
for (var i = 0; i < payload.length; i++) {
sum += payload[i];
}
//send the result to the client
res(null, sum);
});
server.listen(3000);
Client
var client = new conducto.Client();
client.open({secure: false, hostname: localhost, port: 3000});
//wait for socket to open
client.on('open', function() {
/*
* first argument is the method name,
* second the payload, optional
* third is a callback, optional
*/
client.send('sum', 'foo', function(err, res) {
//an err will be received as the payload doesn't comply
console.log(err || res);
//'Payload must be an array.'
});
client.send('sum', [0, 1, 2, 3], function(err, res) {
console.log(err || res);
//6
});
});