1
+ import { Request } from './api.ts'
1
2
import { createHtml } from './html.ts'
2
3
import log from './log.ts'
3
4
import { getContentType } from './mime.ts'
@@ -9,8 +10,8 @@ const commands = {
9
10
'init' : 'Create a new app' ,
10
11
'dev' : 'Start the app in development mode' ,
11
12
'start' : 'Start the app in production mode' ,
12
- 'build' : 'Build& Export a static site' ,
13
- 'upgrade' : 'Upgrade Aleph.js'
13
+ 'build' : 'Build & Export a static site' ,
14
+ 'upgrade' : 'Upgrade Aleph.js command '
14
15
}
15
16
const helpMessage = `Aleph.js v${ version }
16
17
The React Framework in deno.
@@ -123,9 +124,10 @@ async function main() {
123
124
if ( match ) {
124
125
const port = parseInt ( match [ 2 ] )
125
126
listenAndServe ( { port } , async ( req : ServerRequest ) => {
127
+ const url = new URL ( 'http://localhost' + req . url )
128
+ const resp = new Request ( req , { pathname : util . cleanPath ( url . pathname ) , params : { } , query : url . searchParams } )
129
+ const filepath = path . join ( Deno . cwd ( ) , url . pathname )
126
130
try {
127
- const url = new URL ( 'http://localhost' + req . url )
128
- let filepath = path . join ( Deno . cwd ( ) , url . pathname )
129
131
const info = await Deno . lstat ( filepath )
130
132
if ( info . isDirectory ) {
131
133
const r = Deno . readDir ( filepath )
@@ -135,36 +137,19 @@ async function main() {
135
137
items . push ( `<li><a href='${ path . join ( url . pathname , encodeURI ( item . name ) ) } '>${ item . name } ${ item . isDirectory ? '/' : '' } <a></li>` )
136
138
}
137
139
}
138
- req . respond ( {
139
- status : 200 ,
140
- headers : new Headers ( {
141
- 'Content-Type' : 'text/html' ,
142
- 'Content-Length' : info . size . toString ( )
143
- } ) ,
144
- body : createHtml ( {
145
- head : [ `<title>aleph.js/</title>` ] ,
146
- body : `<h1> aleph.js/</h1><ul>${ Array . from ( items ) . join ( '' ) } </ul>`
147
- } )
148
- } )
140
+ resp . send ( createHtml ( {
141
+ head : [ `<title>aleph.js/</title>` ] ,
142
+ body : `<h1> aleph.js/</h1><ul>${ Array . from ( items ) . join ( '' ) } </ul>`
143
+ } ) , 'text/html' )
149
144
return
150
145
}
151
- req . respond ( {
152
- status : 200 ,
153
- headers : new Headers ( { 'Content-Type' : getContentType ( filepath ) } ) ,
154
- body : await Deno . readFile ( filepath )
155
- } )
146
+ resp . send ( await Deno . readFile ( filepath ) , getContentType ( filepath ) )
156
147
} catch ( err ) {
157
148
if ( err instanceof Deno . errors . NotFound ) {
158
- req . respond ( {
159
- status : 404 ,
160
- body : 'not found'
161
- } )
149
+ resp . status ( 404 ) . send ( 'file not found' , 'text/plain' )
162
150
return
163
151
}
164
- req . respond ( {
165
- status : 500 ,
166
- body : err . message
167
- } )
152
+ resp . status ( 500 ) . send ( err . message , 'text/plain' )
168
153
}
169
154
} )
170
155
log . info ( `Proxy https://deno.land/x/aleph on http://localhost:${ port } ` )
0 commit comments