Skip to content

Instantly share code, notes, and snippets.

View Marak's full-sized avatar

Marak

View GitHub Profile
@Marak
Marak / helloWorld.js
Last active August 29, 2015 14:25
hook.io example microservice for saying hello world to a request
// A simple hello world microservice
// Click "Deploy Service" to deploy this code
// Service will respond to HTTP requests with a string
module['exports'] = function helloWorld (hook) {
// hook.req is a Node.js http.IncomingMessage
var host = hook.req.host;
// hook.res is a Node.js httpServer.ServerResponse
// Respond to the request with a simple string
hook.res.end(host + ' says, "Hello world!"');
};
@Marak
Marak / accessRequestParameters.js
Last active August 29, 2015 14:25
hook.io example microservice for accessing incoming http request parameters
// Access incoming HTTP request data
module['exports'] = function accessRequestData (hook) {
var params = hook.params;
// params contains all incoming request parameters,
// such as query string or form data.
// See http://hook.io/docs#data for more information
// Responds back with all incoming HTTP params
hook.res.write(JSON.stringify(hook.params, true, 2));
hook.res.end();
};
@Marak
Marak / inputSchema.js
Created July 25, 2015 02:05
hook.io example microservice for setting schema on hook parameter input
module['exports'] = function inputSchema (hook, callback) {
// Responds back with incoming Hook parameters
hook.res.end(JSON.stringify(hook.params, true, 2));
};
// Specify an optional schema object
// This enables validation and defaults for Hook input
// For complete documentation on available schema types,
// see: http://github.com/mschema/mschema
module['exports'].schema = {
"name": {
@Marak
Marak / transformStream.js
Last active August 29, 2015 14:25
hook.io example microservice for transforming HTTP streams
module['exports'] = function transformStream (hook) {
// If the hook is not currently streaming,
// the req has already been fully buffered,
// and can no longer be streamed!
if (!hook.streaming) {
return hook.res.end('No streaming request detected. \n\nTo test streaming data to this Hook try running this Curl command: \n\n echo "foo" | curl --header "content-type: application/octet-stream" --data-binary @- https://hook.io/Marak/examples-transformStream');
}
hook.req.on('end', function(){
hook.res.end();
});
@Marak
Marak / pipeHook.js
Last active August 29, 2015 14:25
hook.io example microservice for composing two hooks together with stream.pipe()
// Connect two Hooks together using .pipe()
module['exports'] = function pipeHook (hook) {
// hook.open will create a stream to the echo hook
// hook.open is for convience. Any stream will work.
var stream = hook.open('https://hook.io/Marak/examples-helloWorld');
// Once we have created a stream to helloWorld,
// pipe that stream to the client.
// This techinque can be used on any streaming interace
stream.pipe(hook.res);
};
@Marak
Marak / mergeStreams.js
Last active August 29, 2015 14:25
hook.io example microservice for merging streaming together
module['exports'] = function mergeStreams (hook) {
var ms = require('merge-stream');
var stream1 = hook.open('https://hook.io/Marak/examples-helloWorld');
var stream2 = hook.open('https://hook.io/Marak/examples-helloWorld');
var merged = ms(stream1, stream2);
// You can also add new streams later
// merged.add(stream3);
merged.on('data', function(chunk){
hook.res.write(chunk.toString());
});
@Marak
Marak / fakeData.js
Created July 25, 2015 05:47
hook.io example microservice for generating fake data
// Simple microservice for generating fake data
// faker.js docs - http://github.com/marak/faker.js
module['exports'] = function fakeData (hook) {
var faker = require('faker');
// supports multiple locales
faker.locale = 'en'; // try: 'de', 'es'
// supports all faker.js methods
var result = faker.name.findName();
hook.res.end(result);
};
@Marak
Marak / testHook.js
Created August 10, 2015 03:26
My first hook.io microservice
// A simple hello world microservice
// Click "Deploy Service" to deploy this code
// Service will respond to HTTP requests with a string
module['exports'] = function helloWorld (hook) {
// hook.req is a Node.js http.IncomingMessage
var host = hook.req.host;
// hook.res is a Node.js httpServer.ServerResponse
// Respond to the request with a simple string
hook.res.end(host + ' says, "Hello world!"');
};
@Marak
Marak / testHook.js
Created August 11, 2015 00:03
My first hook.io microservice
// A simple hello world microservice
// Click "Deploy Service" to deploy this code
// Service will respond to HTTP requests with a string
module['exports'] = function helloWorld (hook) {
// hook.req is a Node.js http.IncomingMessage
var host = hook.req.host;
// hook.res is a Node.js httpServer.ServerResponse
// Respond to the request with a simple string
hook.res.end(host + ' says, "Hello world!"');
};
module['exports'] = function faker (hook) {
var faker = require('faker');
var params = hook.params;
var parts = params.property.split('.');
faker.locale = hook.params.locale;
var result = faker[parts[0]][parts[1]]();
hook.res.end(JSON.stringify(result, true, 2));