UNPKG

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