1+ /* eslint-disable prefer-regex-literals */
2+
13'use strict'
24
35const gateway = require ( '../index' )
@@ -7,39 +9,47 @@ const PORT = process.env.PORT || 8080
79gateway ( {
810 server : express ( ) ,
911
10- middlewares : [
11- require ( 'cors' ) ( ) ,
12- require ( 'helmet' ) ( )
13- ] ,
14-
15- routes : [ {
16- prefix : '/public' ,
17- target : 'http://localhost:3000' ,
18- docs : {
19- name : 'Public Service' ,
20- endpoint : 'swagger.json' ,
21- type : 'swagger'
12+ middlewares : [ require ( 'cors' ) ( ) , require ( 'helmet' ) ( ) ] ,
13+
14+ routes : [
15+ {
16+ prefix : new RegExp ( '/public/.*' ) , // Express.js v5 requires a RegExp object
17+ // prefix: '/public', // Compatible with Express.js v4,
18+
19+ urlRewrite : ( req ) => req . url . replace ( '/public' , '' ) ,
20+ target : 'http://localhost:3000' ,
21+ docs : {
22+ name : 'Public Service' ,
23+ endpoint : 'swagger.json' ,
24+ type : 'swagger'
25+ }
26+ } ,
27+ {
28+ prefix : new RegExp ( '/admin/.*' ) , // Express.js v5 requires a RegExp object
29+ // prefix: '/admin', // Compatible with Express.js v4,
30+ target : 'http://localhost:3001' ,
31+ middlewares : [
32+ /*
33+ require('express-jwt').expressjwt({
34+ secret: 'shhhhhhared-secret',
35+ algorithms: ['HS256'],
36+ }),
37+ */
38+ ]
2239 }
23- } , {
24- prefix : '/admin' ,
25- target : 'http://localhost:3001' ,
26- middlewares : [
27- require ( 'express-jwt' ) ( {
28- secret : 'shhhhhhared-secret' ,
29- algorithms : [ 'HS256' ]
30- } )
31- ]
32- } ]
40+ ]
3341} ) . listen ( PORT , ( ) => {
3442 console . log ( `API Gateway listening on ${ PORT } port!` )
3543} )
3644
3745const service1 = require ( 'restana' ) ( { } )
3846service1
3947 . get ( '/hi' , ( req , res ) => res . send ( 'Hello World!' ) )
40- . start ( 3000 ) . then ( ( ) => console . log ( 'Public service listening on 3000 port!' ) )
48+ . start ( 3000 )
49+ . then ( ( ) => console . log ( 'Public service listening on 3000 port!' ) )
4150
4251const service2 = require ( 'restana' ) ( { } )
4352service2
44- . get ( '/users' , ( req , res ) => res . send ( [ ] ) )
45- . start ( 3001 ) . then ( ( ) => console . log ( 'Admin service listening on 3001 port!' ) )
53+ . get ( '/admin/users' , ( req , res ) => res . send ( [ ] ) )
54+ . start ( 3001 )
55+ . then ( ( ) => console . log ( 'Admin service listening on 3001 port!' ) )
0 commit comments