@@ -3,7 +3,7 @@ import log from './log.ts'
3
3
import { getContentType } from './mime.ts'
4
4
import { injectHmr , Project } from './project.ts'
5
5
import { path , serve , ws } from './std.ts'
6
- import util , { hashShort } from './util.ts'
6
+ import util , { existsFileSync , hashShort } from './util.ts'
7
7
8
8
export async function start ( appDir : string , port : number , isDev = false ) {
9
9
const project = new Project ( appDir , isDev ? 'development' : 'production' )
@@ -69,22 +69,15 @@ export async function start(appDir: string, port: number, isDev = false) {
69
69
// serve dist files
70
70
if ( pathname . startsWith ( '/_aleph/' ) ) {
71
71
if ( pathname . endsWith ( '.css' ) ) {
72
- try {
73
- const filePath = path . join ( project . buildDir , util . trimPrefix ( pathname , '/_aleph/' ) )
74
- const info = await Deno . lstat ( filePath )
75
- if ( ! info . isDirectory ) {
76
- const body = await Deno . readFile ( filePath )
77
- req . respond ( {
78
- status : 200 ,
79
- headers : new Headers ( { 'Content-Type' : 'text/css; charset=utf-8' } ) ,
80
- body
81
- } )
82
- continue
83
- }
84
- } catch ( err ) {
85
- if ( ! ( err instanceof Deno . errors . NotFound ) ) {
86
- throw err
87
- }
72
+ const filePath = path . join ( project . buildDir , util . trimPrefix ( pathname , '/_aleph/' ) )
73
+ if ( existsFileSync ( filePath ) ) {
74
+ const body = await Deno . readFile ( filePath )
75
+ req . respond ( {
76
+ status : 200 ,
77
+ headers : new Headers ( { 'Content-Type' : 'text/css; charset=utf-8' } ) ,
78
+ body
79
+ } ) . catch ( err => log . warn ( 'ServerRequest.respond:' , err . message ) )
80
+ continue
88
81
}
89
82
} else {
90
83
const reqSourceMap = pathname . endsWith ( '.js.map' )
@@ -125,47 +118,21 @@ export async function start(appDir: string, port: number, isDev = false) {
125
118
'ETag' : mod . hash
126
119
} ) ,
127
120
body
128
- } )
121
+ } ) . catch ( err => log . warn ( 'ServerRequest.respond:' , err . message ) )
129
122
continue
130
123
}
131
124
}
132
- req . respond ( {
133
- status : 404 ,
134
- headers : new Headers ( { 'Content-Type' : 'text/html' } ) ,
135
- body : createHtml ( {
136
- lang : 'en' ,
137
- head : [ '<title>404 - not found</title>' ] ,
138
- body : '<p><strong><code>404</code></strong><small> - </small><span>not found</span></p>'
139
- } )
140
- } )
141
- continue
142
125
}
143
126
144
127
// serve public files
145
- try {
146
- const filePath = path . join ( project . appRoot , 'public' , pathname )
147
- const info = await Deno . lstat ( filePath )
148
- if ( ! info . isDirectory ) {
149
- const body = await Deno . readFile ( filePath )
150
- req . respond ( {
151
- status : 200 ,
152
- headers : new Headers ( { 'Content-Type' : getContentType ( filePath ) } ) ,
153
- body
154
- } )
155
- continue
156
- }
157
- } catch ( err ) {
158
- if ( ! ( err instanceof Deno . errors . NotFound ) ) {
159
- throw err
160
- }
161
- }
162
-
163
- if ( pathname === '/favicon.ico' ) {
128
+ const filePath = path . join ( project . appRoot , 'public' , pathname )
129
+ if ( existsFileSync ( filePath ) ) {
130
+ const body = await Deno . readFile ( filePath )
164
131
req . respond ( {
165
- status : 404 ,
166
- headers : new Headers ( { 'Content-Type' : 'text/plain' } ) ,
167
- body : 'icon not found'
168
- } )
132
+ status : 200 ,
133
+ headers : new Headers ( { 'Content-Type' : getContentType ( filePath ) } ) ,
134
+ body
135
+ } ) . catch ( err => log . warn ( 'ServerRequest.respond:' , err . message ) )
169
136
continue
170
137
}
171
138
@@ -175,7 +142,7 @@ export async function start(appDir: string, port: number, isDev = false) {
175
142
status,
176
143
headers : new Headers ( { 'Content-Type' : 'text/html' } ) ,
177
144
body : html
178
- } )
145
+ } ) . catch ( err => log . warn ( 'ServerRequest.respond:' , err . message ) )
179
146
} catch ( err ) {
180
147
req . respond ( {
181
148
status : 500 ,
@@ -185,7 +152,7 @@ export async function start(appDir: string, port: number, isDev = false) {
185
152
head : [ '<title>500 - internal server error</title>' ] ,
186
153
body : `<p><strong><code>500</code></strong><small> - </small><span>${ err . message } </span></p>`
187
154
} )
188
- } )
155
+ } ) . catch ( err => log . warn ( 'ServerRequest.respond:' , err . message ) )
189
156
}
190
157
}
191
158
} catch ( err ) {
0 commit comments