-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdisplayRoutes.js
46 lines (40 loc) · 1.12 KB
/
displayRoutes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const fs = require('fs');
const chalk = require('chalk');
const listEndpoints = require('express-list-endpoints');
const Table = require('cli-table');
module.exports = function(app, filename) {
let table = new Table({
head: ['METHOD', 'ROUTE'],
colWidths: [25, 50]
});
listEndpoints(app).forEach((endpoint) => {
endpoint.methods = makeMethodsColorsful(endpoint.methods);
table.push([endpoint.methods.join(' '), endpoint.path]);
});
if (typeof filename === 'string') {
return fs.writeFile(filename, table.toString(), (error) => {
if (error) throw error;
console.log(`Printed route table to ${filename}`);
});
}
console.log(table.toString());
};
function makeMethodsColorsful(methods) {
methods.forEach((method, index) => {
switch(method) {
case 'GET':
methods[index] = chalk.yellow(method);
break;
case 'POST':
methods[index] = chalk.green(method);
break;
case 'DELETE':
methods[index] = chalk.red(method);
break;
case 'PUT':
methods[index] = chalk.cyan(method);
break;
}
});
return methods;
}