Skip to content

Commit f5cae0a

Browse files
authored
Adding RegExp support in route prefix (#95)
* Supporting RegExp in route prefix * Updating documentation about Express v5 compatibility
1 parent 57f87cb commit f5cae0a

14 files changed

+700
-425
lines changed

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.yaml
2+
*.yml

.prettierrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"semi": false
5+
}

demos/basic-express.js

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable prefer-regex-literals */
2+
13
'use strict'
24

35
const gateway = require('../index')
@@ -7,39 +9,47 @@ const PORT = process.env.PORT || 8080
79
gateway({
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

3745
const service1 = require('restana')({})
3846
service1
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

4251
const service2 = require('restana')({})
4352
service2
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

Comments
 (0)