1+ import express , { Request , Response , NextFunction } from 'express'
2+ import axios from 'axios'
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 ( '/products' , express . json ( { limit : 100 } ) )
45+
46+ // Error handling Middleware functions
47+ const errorLogger = ( error : Error , request : Request , response : Response , next : NextFunction ) => {
48+ console . log ( `error ${ error . message } ` )
49+ next ( error ) // calling next middleware
50+ }
51+
52+ const errorResponder = ( error : AppError , request : Request , response : Response , next : NextFunction ) => {
53+ response . header ( "Content-Type" , 'application/json' )
54+
55+ const status = error . statusCode || 400
56+ response . status ( status ) . send ( error . message )
57+ }
58+
59+ const invalidPathHandler = ( request : Request , response : Response , next : NextFunction ) => {
60+ response . status ( 400 )
61+ response . send ( 'invalid path' )
62+ }
63+
64+
65+ // handle post request for path /products
66+ app . post ( '/products' , ( request : Request , response : Response ) => {
67+ const products = [ ]
68+
69+ const name = request . body . name
70+
71+ const brand = request . body . brand
72+
73+ const category = request . body . category
74+
75+ if ( name == null ) {
76+ response . status ( 500 ) . json ( { message : "Mandatory field name is missing. " } )
77+ } else {
78+ console . log ( name + " " + brand )
79+
80+ products . push ( { name : request . body . name , brand : request . body . brand , price : request . body . price } )
81+
82+ const productCreationResponse = { productID : "12345" , result : "success" }
83+ response . json ( productCreationResponse )
84+ }
85+ } )
86+
87+ app . get ( '/products' , async ( request : Request , response : Response , next : NextFunction ) => {
88+ try {
89+ const apiResponse = await axios . get ( "http://localhost:3001/products" )
90+
91+ const jsonResponse = apiResponse . data
92+ console . log ( "response " + jsonResponse )
93+
94+ response . send ( jsonResponse )
95+ } catch ( error ) {
96+ next ( error )
97+ }
98+
99+ } )
100+
101+ app . get ( '/product' , ( request : Request , response : Response , next : NextFunction ) => {
102+
103+ axios . get ( "http://localhost:3001/product" )
104+ . then ( jsonresponse => response . send ( jsonresponse ) )
105+ . catch ( next )
106+ } )
107+
108+ app . get ( '/productswitherror' , ( request , response ) => {
109+ let error :AppError = new AppError ( 400 , `processing error in request at ${ request . url } ` )
110+ error . statusCode = 400
111+ throw error
112+ } )
113+
114+ app . get ( '/productswitherror' , ( request : Request , response : Response ) => {
115+ let error : AppError = new AppError ( 400 , `processing error in request at ${ request . url } ` )
116+
117+ throw error
118+ } )
119+
120+ app . use ( errorLogger )
121+ app . use ( errorResponder )
122+ app . use ( invalidPathHandler )
123+
124+ app . listen ( port , ( ) => {
125+ console . log ( `Server listening at port ${ port } .` )
126+ } )
0 commit comments