Skip to content

Instantly share code, notes, and snippets.

@abtris
Last active October 4, 2015 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abtris/6aca9b2668b8b5af0208 to your computer and use it in GitHub Desktop.
Save abtris/6aca9b2668b8b5af0208 to your computer and use it in GitHub Desktop.
Example Simple NodeJS Application for Docker demo

Demo steps

  1. Install plugin - heroku plugins:install heroku-docker
  2. Clone this repository - git clone https://gist.github.com/6aca9b2668b8b5af0208.git
  3. Init - heroku docker:init
  4. Build containers - docker-compose build
  5. Run containers - docker-compose run web
  6. Test at localhost - curl "http://$(docker-machine ip dev):8080"
  7. Create heroku app - heroku create
  8. Create heroku slug and upload to heroku - heroku docker:release
  9. Open app at heroku - heroku open
{
"image": "heroku/nodejs"
}
var Hapi = require('hapi');
var server = new Hapi.Server();
var port = process.env.PORT || 3001;
server.connection({port: port});
// Polls API Root
// Retrieve the Entry Point
server.route({
method: 'GET',
path:'/',
handler: function(request, reply) {
reply({"questions_url": "/questions"});
}
}
);
// Group Question
// Question
// View a Questions Detail
server.route({
method: 'GET',
path: '/questions/{question_id}',
handler: function(request, reply) {
reply({"question":"Favourite programming language?", "published_at":"2014-11-11T08:40:51.620Z", "url":"/questions/1", "choices":[{"choice":"Swift", "url":"/questions/1/choices/1", "votes":2048}, {"choice":"Python", "url":"/questions/1/choices/2", "votes":1024}, {"choice":"Objective-C", "url":"/questions/1/choices/3", "votes":512}, {"choice":"Ruby", "url":"/questions/1/choices/4", "votes":256}]});
}
});
// Choice
// Vote on a Choice
server.route({
method: 'POST',
path: '/questions/{question_id}/choices/{choice_id}',
handler: function(request, reply) {
reply().code(201).header('Location', '/questions/' + request.params.question_id);
}
});
// Questions Collection
// List All Questions
server.route({
method: 'GET',
path: '/questions',
handler: function(request, reply) {
reply([{"question":"Favourite programming language?", "published_at":"2014-11-11T08:40:51.620Z", "url":"/questions/1", "choices":[{"choice":"Swift", "url":"/questions/1/choices/1", "votes":2048}, {"choice":"Python", "url":"/questions/1/choices/2", "votes":1024}, {"choice":"Objective-C", "url":"/questions/1/choices/3", "votes":512}, {"choice":"Ruby", "url":"/questions/1/choices/4", "votes":256}]}])
.header('Link', '</questions?page=' + (parseInt(request.params.page, 10) + 1) + '>; rel="next"');
}
});
// Create a New Question
server.route({
method: 'POST',
path: '/questions',
handler: function(request, reply) {
reply({"question":"Favourite programming language?", "published_at":"2014-11-11T08:40:51.620Z", "url":"/questions/2", "choices":[{"choice":"Swift", "url":"/questions/2/choices/1", "votes":0}, {"choice":"Python", "url":"/questions/2/choices/2", "votes":0}, {"choice":"Objective-C", "url":"/questions/2/choices/3", "votes":0}, {"choice":"Ruby", "url":"/questions/2/choices/4", "votes":0}]})
.code(201)
.header('Location', '/questions/2');
}
});
// Start the server
server.start(function() {
console.log('Server running at:', server.info.uri);
});
{
"name": "hapi",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"hapi": "^8.4.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT"
}
web: node index.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment