functional-services

0.0.4 • Public • Published

functional-services Build Status

NodeJS Functional Service RPC Server

Install

  npm install functional-services

Examples:

Initialize (INIT):

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);

Options:

  • keys: AES Keys
  • services: Domaindriven name and function

Service:

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 Send null if no error.
  • response (optional) Response Payload
var services = {
	'/namespace/action': (req, callback) => {
		callback(null, 'Hello '+req.payload.name);
	}
};

Connect

Client NodeJS

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)));

Server ExpressJS

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() {});

Server HTTP

// 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.')
});

Contributors

Philipp

The MIT License (MIT)

Dependents (0)

Package Sidebar

Install

npm i functional-services

Weekly Downloads

0

Version

0.0.4

License

MIT

Last publish

Collaborators

  • pkuebler