Skip to content

Instantly share code, notes, and snippets.

View Marak's full-sized avatar

Marak

View GitHub Profile
module['exports'] = function echoHttp (hook) {
if (!hook.streaming) {
return hook.res.end('No streaming request detected.');
}
hook.req.on('end', function(){
hook.res.end();
});
hook.req.on('data', function(chunk){
hook.res.write(chunk.toString())
@Marak
Marak / testHook.js
Created August 20, 2015 01:49
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;
console.log('incoming', hook.req.url);
// 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 / highfive.js
Last active August 29, 2015 14:27 — forked from kmoe/highfive.js
module['exports'] = function highFive(hook) {
// hook.io has a range of node modules available - see
// https://hook.io/modules.
// We use request (https://www.npmjs.com/package/request) for an easy way to
// make the HTTP request.
var request = require('request');
// The parameters passed in via the slash command POST request.
var params = hook.params;
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));
@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!"');
};
@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 / 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 / imageResize.js
Last active July 31, 2019 19:04
hook.io example microservice for resizing images
module['exports'] = function imageResize (hook, callback) {
// GraphicsMagick fully supported
var gm = require('gm');
// for a more complete example that supports file uploads and streaming uploads
// see: http://image.resize.hook.io
// grab an image as a url
// no file has been uploaded, fallback to the image "url" parameter
var stream = hook.open('https://hook.io/img/robotcat.png');
hook.res.writeHead(200, { 'Content-Type': 'image/png' });
gm(stream)
@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 / 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);
};