| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- const PORT = process.env.PORT || 1776;
- const DIV = `_________________________________________________________________`;
- const ASCIIART = `watch-finished`;
- const SLUG = 'App up and running';
- const HOSTNAME = 'localhost';
- const apitable = (baseUrl, routes) => {
- let Table = require('cli-table');
- let table = new Table({
- style: { head: ['green'] },
- head: ['Method', 'Path'],
- });
- for (let key in routes) {
- if (routes && routes[key]) {
- let val = routes[key];
- let _o = {};
- let method = val.stack[0].method;
- if (!method || method === undefined || method === 'undefined') method = 'all';
- _o[method] = [baseUrl + val.path];
- table.push(_o);
- }
- }
- console.log(table.toString());
- return table;
- };
- module.exports = app => {
- console.log(`${DIV}\n${ASCIIART}\n\n${SLUG}\n`);
- console.log(` http://${HOSTNAME}:${PORT}`);
- let routes = [];
- app._router.stack.forEach(middleware => {
- if (middleware.route) {
- // routes registered directly on the app
- routes.push(middleware.route);
- } else if (middleware.name === 'router') {
- // router middleware
- middleware.handle.stack.forEach(function(handler) {
- let route = handler.route;
- route && routes.push(route);
- });
- }
- });
- if (routes) {
- console.log('\nAPI Routes\n');
- apitable('', routes);
- }
- console.log('\n');
- };
|