This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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!"'); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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!"'); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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!"'); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |