Skip to content

Instantly share code, notes, and snippets.

View echo-stream.js
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 Aug 20, 2015
My first hook.io microservice
View testHook.js
// 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!"');
View 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;
View fakerNew.js
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 Aug 11, 2015
My first hook.io microservice
View testHook.js
// 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 Aug 10, 2015
My first hook.io microservice
View testHook.js
// 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 Jul 25, 2015
hook.io example microservice for generating fake data
View fakeData.js
// 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 Jul 31, 2019
hook.io example microservice for resizing images
View imageResize.js
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 Aug 29, 2015
hook.io example microservice for merging streaming together
View mergeStreams.js
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 Aug 29, 2015
hook.io example microservice for composing two hooks together with stream.pipe()
View pipeHook.js
// 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);
};