index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // simple node web server that displays hello world
  2. // optimized for Docker image
  3. var express = require('express');
  4. // this example uses express web framework so we know what longer build times
  5. // do and how Dockerfile layer ordering matters. If you mess up Dockerfile ordering
  6. // you'll see long build times on every code change + build. If done correctly,
  7. // code changes should be only a few seconds to build locally due to build cache.
  8. var morgan = require('morgan');
  9. // morgan provides easy logging for express, and by default it logs to stdout
  10. // which is a best practice in Docker. Friends don't let friends code their apps to
  11. // do app logging to files in containers.
  12. // Constants
  13. const PORT = process.env.PORT || 8080;
  14. // if you're not using docker-compose for local development, this will default to 8080
  15. // to prevent non-root permission problems with 80. Dockerfile is set to make this 80
  16. // because containers don't have that issue :)
  17. // Appi
  18. var app = express();
  19. app.use(morgan('common'));
  20. app.get('/', function (req, res) {
  21. res.send('Hello Docker World\n');
  22. });
  23. app.get('/healthz', function (req, res) {
  24. // do app logic here to determine if app is truly healthy
  25. // you should return 200 if healthy, and anything else will fail
  26. // if you want, you should be able to restrict this to localhost (include ipv4 and ipv6)
  27. res.send('I am happy and healthy\n');
  28. });
  29. var server = app.listen(PORT, function () {
  30. console.log('Webserver is ready');
  31. });
  32. //
  33. // need this in docker container to properly exit since node doesn't handle SIGINT/SIGTERM
  34. // this also won't work on using npm start since:
  35. // https://github.com/npm/npm/issues/4603
  36. // https://github.com/npm/npm/pull/10868
  37. // https://github.com/RisingStack/kubernetes-graceful-shutdown-example/blob/master/src/index.js
  38. // if you want to use npm then start with `docker run --init` to help, but I still don't think it's
  39. // a graceful shutdown of node process
  40. //
  41. // quit on ctrl-c when running docker in terminal
  42. process.on('SIGINT', function onSigint () {
  43. console.info('Got SIGINT (aka ctrl-c in docker). Graceful shutdown ', new Date().toISOString());
  44. shutdown();
  45. });
  46. // quit properly on docker stop
  47. process.on('SIGTERM', function onSigterm () {
  48. console.info('Got SIGTERM (docker container stop). Graceful shutdown ', new Date().toISOString());
  49. shutdown();
  50. })
  51. // shut down server
  52. function shutdown() {
  53. server.close(function onServerClosed (err) {
  54. if (err) {
  55. console.error(err);
  56. process.exitCode = 1;
  57. }
  58. process.exit();
  59. })
  60. }
  61. //
  62. // need above in docker container to properly exit
  63. //
  64. module.exports = app;