motd.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const PORT = process.env.PORT || 1776;
  2. const DIV = `_________________________________________________________________`;
  3. const ASCIIART = `watch-finished`;
  4. const SLUG = 'App up and running';
  5. const HOSTNAME = 'localhost';
  6. const apitable = (baseUrl, routes) => {
  7. let Table = require('cli-table');
  8. let table = new Table({
  9. style: { head: ['green'] },
  10. head: ['Method', 'Path'],
  11. });
  12. for (let key in routes) {
  13. if (routes && routes[key]) {
  14. let val = routes[key];
  15. let _o = {};
  16. let method = val.stack[0].method;
  17. if (!method || method === undefined || method === 'undefined') method = 'all';
  18. _o[method] = [baseUrl + val.path];
  19. table.push(_o);
  20. }
  21. }
  22. console.log(table.toString());
  23. return table;
  24. };
  25. module.exports = app => {
  26. console.log(`${DIV}\n${ASCIIART}\n\n${SLUG}\n`);
  27. console.log(` http://${HOSTNAME}:${PORT}`);
  28. let routes = [];
  29. app._router.stack.forEach(middleware => {
  30. if (middleware.route) {
  31. // routes registered directly on the app
  32. routes.push(middleware.route);
  33. } else if (middleware.name === 'router') {
  34. // router middleware
  35. middleware.handle.stack.forEach(function(handler) {
  36. let route = handler.route;
  37. route && routes.push(route);
  38. });
  39. }
  40. });
  41. if (routes) {
  42. console.log('\nAPI Routes\n');
  43. apitable('', routes);
  44. }
  45. console.log('\n');
  46. };