npm install functional-services
var FuncServices = require( "functional-services" );
var keys = {
'public_key': 'secret_key'
};
var services = {
'/namespace/action': (req, callback) => {
callback(null, 'Hello '+req.payload.name);
}
};
var funcServices = FuncServices(keys, services);
-
keys
: AES Keys -
services
: Domaindriven name and function
var services = {
'[ domain ]': (request, callback) => {
// request
// {
// url: [ domain ],
// public_id: [ public_key ],
// payload: [ payload ]
// }
callback([ error ], [ response ]);
}
};
Run service.
Note: Need a value
or a (global/options) timeOutFunction
. If the value
is empty, it is loaded from the timeOutFunction
.
-
domain
Address of the service -
public_key
The public key sent along. -
payload
Payload -
error
Sendnull
if no error. -
response
(optional) Response Payload
var services = {
'/namespace/action': (req, callback) => {
callback(null, 'Hello '+req.payload.name);
}
};
Send request to Server.
// Private / Public Key Storage
var keys = {
'public_key': 'private_key'
};
// Get Client / Server
funcServices = FuncServices(keys, {});
// HTTP Request Options
var options = {
host: 'localhost',
path: '/',
port: port,
method: 'POST'
};
// Sending Data
var data = {
public_id: 'public_key',
// Payload
ciphered: {
url: "/demos/helloworld", // Router Service Address
public_id: 'public_key',
payload: {name: 'Peter'} // Payload Params
}
};
// HTTP Request Callback
var callback = function(response) {
var str = ''
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
var raw = JSON.parse(str);
var r = funcServices.decrypt(raw);
console.log(r);
});
}
var req = http.request(options, callback);
req.end(JSON.stringify(funcServices.encrypt(data)));
Use ExpressJS
var express = require('express');
var app = express();
var funcServices = FuncServices(keys, services);
var middleware = funcServices.express();
app.use('/api', middleware);
app.listen(3000, function() {});
// HTTP Server
const requestHandler = (request, response) => {
// Get Chunks
var fullBody = '';
request.on('data', function(chunk) {
fullBody += chunk.toString();
});
// Request finish
request.on('end', function() {
// Get JSON
var body = JSON.parse(fullBody);
// Decode, Route, Run, Encrypt, Response
funcServices.router(body, (err, res) => {
// Send
response.end(JSON.stringify(res));
});
});
}
// Start Server
const server = http.createServer(requestHandler);
server.listen(port, (err) => {
console.log('Server run.')
});