1+ import { Request } from './api.ts'
12import { createHtml } from './html.ts'
23import log from './log.ts'
34import { getContentType } from './mime.ts'
@@ -9,8 +10,8 @@ const commands = {
910 'init' : 'Create a new app' ,
1011 'dev' : 'Start the app in development mode' ,
1112 '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 '
1415}
1516const helpMessage = `Aleph.js v${ version }
1617The React Framework in deno.
@@ -123,9 +124,10 @@ async function main() {
123124 if ( match ) {
124125 const port = parseInt ( match [ 2 ] )
125126 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 )
126130 try {
127- const url = new URL ( 'http://localhost' + req . url )
128- let filepath = path . join ( Deno . cwd ( ) , url . pathname )
129131 const info = await Deno . lstat ( filepath )
130132 if ( info . isDirectory ) {
131133 const r = Deno . readDir ( filepath )
@@ -135,36 +137,19 @@ async function main() {
135137 items . push ( `<li><a href='${ path . join ( url . pathname , encodeURI ( item . name ) ) } '>${ item . name } ${ item . isDirectory ? '/' : '' } <a></li>` )
136138 }
137139 }
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' )
149144 return
150145 }
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 ) )
156147 } catch ( err ) {
157148 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' )
162150 return
163151 }
164- req . respond ( {
165- status : 500 ,
166- body : err . message
167- } )
152+ resp . status ( 500 ) . send ( err . message , 'text/plain' )
168153 }
169154 } )
170155 log . info ( `Proxy https://deno.land/x/aleph on http://localhost:${ port } ` )
0 commit comments