Skip to content

Instantly share code, notes, and snippets.

View Marak's full-sized avatar

Marak

View GitHub Profile
@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 / 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 / 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 / simpleHttpRequest.js
Last active December 26, 2015 20:29
hook.io example microservice for sending outgoing http requests
module['exports'] = function simpleHttpRequest (hook) {
// npm modules available, see: http://hook.io/modules
var request = require('request');
request.get('http://httpbin.org/ip', function(err, res, body){
if (err) {
return hook.res.end(err.messsage);
}
hook.res.end(body);
})
};
@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 / datastoreExample.js
Last active June 18, 2016 04:17
hook.io example microservice for using hook.io's cloud datastore API
module['exports'] = function datastoreExample (hook) {
var res = hook.res,
req = hook.req,
store = hook.datastore;
store.set('mykey', { foo: "bar" }, function(err, result){
if (err) { return res.end(err.message); }
store.get('mykey', function(err, result){
if (err) { return res.end(err.message); }
res.end(JSON.stringify(result, true, 2));
});
@Marak
Marak / composeHook.js
Created July 24, 2015 06:19
hook.io example microservice for composing multiple services
module['exports'] = function (hook) {
hook.res.end('ended');
};
module['exports'].inputs = ['echo', 'echo', 'echo'];
module['exports'].outputs = ['echo', 'echo', 'echo'];
@Marak
Marak / testHook.js
Created July 23, 2015 03:41
My first hook.io microservice
module['exports'] = function echo (hook) {
// Access Node.js HTTP Request and Response objects
var req = hook.req,
res = hook.res;
// Use NPM Modules
var faker = require('faker');
res.write('<strong>BYE BILLY</strong> ');
res.write(req.headers["x-forwarded-for"] + '<br/>');
res.end(faker.hacker.phrase());
};
@Marak
Marak / gatewayHook.js
Last active August 29, 2015 14:25
hook.io microservice for executing arbitrary functions without an account
// gateway hook for running microservices on hook.io in real-time without an account
// example of deploying a js file to the hook.io cloud from the command line using cURL
// cat echo.js | curl --data-urlencode source@- http://gateway.hook.io
module['exports'] = function gatewayHook (hook) {
var mschema = require('mschema');
var params = hook.params;
var source = params.source;
@Marak
Marak / badTimeOutHook.js
Last active August 29, 2015 14:24
hook.io hook with timeout error ( never ends )
module['exports'] = function badTimeOutHook (hook) {
hook.res.write('hello');
// never calls hook.res.end()
};