@@ -11,11 +11,14 @@ export default function createRouter (options) {
11
11
12
12
// for multiple content types, show the content configuration in the root request
13
13
if ( ! options . content [ '/' ] ) {
14
- router . use ( "/" , new Router ( ) . get ( '/' , ( req , res ) => {
15
- response ( res ) . json ( {
16
- 'content-endpoints' : Object . keys ( options . content )
14
+ router . use (
15
+ '/' ,
16
+ new Router ( ) . get ( '/' , ( req , res ) => {
17
+ response ( res ) . json ( {
18
+ 'content-endpoints' : Object . keys ( options . content )
19
+ } )
17
20
} )
18
- } ) )
21
+ )
19
22
}
20
23
21
24
Object . keys ( options . content ) . forEach ( dirName => {
@@ -41,30 +44,32 @@ function curryResponseHandler (endpoint, options) {
41
44
42
45
logRequest ( permalink , api . serverPrefix , api . baseURL )
43
46
44
- if ( permalink === '/' ) { // request multiple pages from directory
47
+ if ( permalink === '/' ) {
48
+ // request multiple pages from directory
45
49
if ( between ) send . json ( db . findBetween ( between , query ) )
46
50
else if ( only ) send . json ( db . findOnly ( only , query ) )
47
51
else send . json ( db . findAll ( query ) )
48
- } else { // request single page
52
+ } else {
53
+ // request single page
49
54
if ( db . exists ( permalink ) ) send . json ( db . find ( permalink , query ) )
50
55
else send . notFound ( )
51
56
}
52
57
}
53
58
}
54
59
55
- export const response = ( res ) => ( {
56
- json ( data ) {
60
+ export const response = res => ( {
61
+ json ( data ) {
57
62
res . setHeader ( 'Content-Type' , 'application/json' )
58
63
res . end ( JSON . stringify ( data ) , 'utf-8' )
59
64
console . log ( ` Response sent successfully.` )
60
65
} ,
61
- error ( err ) {
66
+ error ( err ) {
62
67
console . log ( ` Failed to send response.` , error )
63
68
res . statusCode = 500
64
69
res . statusMessage = 'Internal Server Error'
65
70
res . end ( err . stack || String ( err ) )
66
71
} ,
67
- notFound ( ) {
72
+ notFound ( ) {
68
73
console . log ( ` Page not found.` )
69
74
res . statusCode = 404
70
75
res . statusMessage = 'Not Found'
@@ -73,6 +78,8 @@ export const response = (res) => ({
73
78
} )
74
79
75
80
function logRequest ( permalink , apiPrefix , baseURL ) {
76
- console . log ( `${ chalk . blue ( apiPrefix ) } ${ chalk . green ( 'GET' ) } ${ baseURL + permalink } ` )
81
+ console . log (
82
+ `${ chalk . blue ( apiPrefix ) } ${ chalk . green ( 'GET' ) } ${ baseURL + permalink } `
83
+ )
77
84
return permalink
78
85
}
0 commit comments