server.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // simple node web server that displays hello world
  2. // optimized for Docker image
  3. const 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. const 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. const database = require("./database");
  13. // Appi
  14. const app = express();
  15. app.use(morgan("common"));
  16. app.get("/", function(req, res, next) {
  17. database.raw('select VERSION() version')
  18. .then(([rows, columns]) => rows[0])
  19. .then((row) => res.json({ message: `Hello from MySQL ${row.version}` }))
  20. .catch(next);
  21. });
  22. app.get("/healthz", function(req, res) {
  23. // do app logic here to determine if app is truly healthy
  24. // you should return 200 if healthy, and anything else will fail
  25. // if you want, you should be able to restrict this to localhost (include ipv4 and ipv6)
  26. res.send("I am happy and healthy\n");
  27. });
  28. module.exports = app;