import { IncomingMessage , ServerResponse } from 'http' ; import * as nodeUrl from 'url' ; export class RequestHandler { public constructor ( private logger , private router ) { } public process ( req , res ) { const method = < string > req . method ; const url = nodeUrl . parse ( < string > req . url , true ) ; let targetHttpController ; try { targetHttpController = < Function > ( < Map < string , Function > > this . router . get ( method ) ) . get ( < string > url . pathname ) ; if ( ! targetHttpController ) { throw new Error ( 'Resource not in router' ) ; } } catch ( parseException ) { this . logger . warning ( '404 Not Found' , parseException ) ; res . writeHead ( 404 ) ; res . end ( 'HTTP/1.1 404 Not Found\r\n\r\n' ) ; return ; } targetHttpController ( req , res ) ; } }