1+ import express , { Request , Response , NextFunction } from 'express'
2+ import morgan from 'morgan'
3+
4+ const app = express ( )
5+ const port :number = 3000
6+
7+
8+ interface Product {
9+
10+ name : string
11+ price : number
12+ brand : string
13+ category ?: string
14+ }
15+
16+ interface ProductCreationResponse {
17+ productID : string
18+ result : string
19+ }
20+
21+
22+ class AppError extends Error {
23+ statusCode : number ;
24+
25+ constructor ( statusCode : number , message : string ) {
26+ super ( message ) ;
27+
28+ Object . setPrototypeOf ( this , new . target . prototype ) ;
29+ this . name = Error . name ;
30+ this . statusCode = statusCode ;
31+ Error . captureStackTrace ( this ) ;
32+ }
33+ }
34+
35+ const requestLogger = ( request : Request , response : Response , next : NextFunction ) => {
36+ console . log ( `${ request . method } url:: ${ request . url } ` ) ;
37+ next ( )
38+ }
39+
40+ app . use ( express . static ( 'images' ) )
41+ app . use ( express . static ( 'htmls' ) )
42+ app . use ( requestLogger )
43+
44+ app . use ( morgan ( 'tiny' ) )
45+
46+ app . use ( '/products' , express . json ( { limit : 100 } ) )
47+
48+ // Error handling Middleware functions
49+ const errorLogger = ( error : Error , request : Request , response : Response , next : NextFunction ) => {
50+ console . log ( `error ${ error . message } ` )
51+ next ( error ) // calling next middleware
52+ }
53+
54+ const errorResponder = ( error : AppError , request : Request , response : Response , next : NextFunction ) => {
55+ response . header ( "Content-Type" , 'application/json' )
56+
57+ const status = error . statusCode || 400
58+ response . status ( status ) . send ( error . message )
59+ }
60+ const invalidPathHandler = ( request : Request , response : Response , next : NextFunction ) => {
61+ response . status ( 400 )
62+ response . send ( 'invalid path' )
63+ }
64+
65+
66+
67+ app . get ( 'product' , ( request : Request , response : Response ) => {
68+ response . sendFile ( "productsample.html" )
69+ } )
70+
71+ // handle get request for path /
72+ app . get ( '/' , ( request : Request , response : Response ) => {
73+ response . send ( 'response for GET request' ) ;
74+ } )
75+
76+
77+ const requireJsonContent = ( request : Request , response : Response , next : NextFunction ) => {
78+ if ( request . headers [ 'content-type' ] !== 'application/json' ) {
79+ response . status ( 400 ) . send ( 'Server requires application/json' )
80+ } else {
81+ next ( )
82+ }
83+ }
84+
85+
86+ const addProducts = ( request : Request , response : Response , next : NextFunction ) => {
87+ let products : Product [ ] = [ ]
88+
89+ const name : string = request . body . name
90+
91+ const brand : string = request . body . brand
92+
93+ const category : string = request . body . category
94+
95+ console . log ( name + " " + brand )
96+
97+ products . push ( { name : request . body . name , brand : request . body . brand , price : request . body . price } )
98+
99+ const productCreationResponse : ProductCreationResponse = { productID : "12345" , result : "success" }
100+ response . json ( productCreationResponse )
101+
102+ response . status ( 200 ) . json ( products ) ;
103+ }
104+ app . post ( '/products' , addProducts )
105+
106+ app . get ( '/productswitherror' , ( request : Request , response : Response ) => {
107+ let error : AppError = new AppError ( 400 , `processing error in request at ${ request . url } ` )
108+
109+ throw error
110+ } )
111+
112+ app . use ( errorLogger )
113+ app . use ( errorResponder )
114+ app . use ( invalidPathHandler )
115+
116+ app . listen ( port , ( ) => {
117+ console . log ( `Server listening at port ${ port } .` )
118+ } )
0 commit comments