1 | 'use strict'
|
2 |
|
3 | const colors = require('./detect/colors')
|
4 | const logError = require('./logError')
|
5 |
|
6 | const onRedirected = ({ method, url, statusCode, timeSpent }) => {
|
7 | let report
|
8 | if (!statusCode) {
|
9 | report = colors.red('N/A')
|
10 | } else if (statusCode > 399) {
|
11 | report = colors.red(statusCode.toString())
|
12 | } else {
|
13 | report = colors.green(statusCode.toString())
|
14 | }
|
15 | report += colors.magenta(` ${timeSpent} ms`)
|
16 | console.log(colors.magenta('SERVE'), colors.gray(method), colors.gray(url), report)
|
17 | }
|
18 |
|
19 | const onRedirecting = ({ method, url, type, redirect }) => {
|
20 | let redirectLabel
|
21 | if (typeof redirect === 'function') {
|
22 | redirectLabel = '(function)'
|
23 | } else {
|
24 | redirectLabel = redirect.toString()
|
25 | }
|
26 | console.log(colors.gray('RDRCT'), colors.gray(method), colors.gray(url), colors.gray('\n\\____'), colors.gray(type),
|
27 | colors.gray(redirectLabel))
|
28 | }
|
29 |
|
30 | module.exports = (serve, verbose) => {
|
31 | serve
|
32 | .on('ready', ({ url }) => {
|
33 | console.log(colors.yellow(`Server running at ${url}`))
|
34 | })
|
35 | .on('error', logError)
|
36 | .on('redirected', onRedirected)
|
37 | if (verbose) {
|
38 | serve.on('redirecting', onRedirecting)
|
39 | }
|
40 | return serve
|
41 | }
|