Skip to content

Commit b65b602

Browse files
authored
Merge pull request #21 from jkyberneees/framework-agnostic
Framework agnostic
2 parents eb7ffbe + 7e502b9 commit b65b602

File tree

5 files changed

+469
-14
lines changed

5 files changed

+469
-14
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# fast-gateway
2-
A super fast Node.js API Gateway for the masses!
2+
A super fast, framework agnostic Node.js API Gateway for the masses ❤️
33

44
## Medium articles:
55
- https://medium.com/@kyberneees/node-js-api-gateway-a-developer-perspective-8defe575ed21
@@ -35,8 +35,16 @@ service.start(3000)
3535
## Configuration options explained
3636
```js
3737
{
38+
// Optional server instance. Any HTTP framework that supports the following signature is compatible:
39+
// - server[HTTP_METHOD](pattern, [middleware1, middleware2,], handler)
40+
//
41+
// Known compatible frameworks: Restana, Express.js
42+
// If omitted, restana is used as default HTTP framework
43+
server,
3844
// Optional restana library configuration (https://www.npmjs.com/package/restana#configuration)
39-
// If the given value is a function instead of an object, it will be considered a restana service factory.
45+
// If the given value is a function instead of an object, it will be considered a restana server factory.
46+
//
47+
// If "server" is provided, this settings are ignored.
4048
restana: {},
4149
// Optional global middlewares in the format: (req, res, next) => next()
4250
// Default value: []

demos/basic-express.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const gateway = require('../index')
2+
const express = require('express')
3+
const PORT = process.env.PORT || 8080
4+
5+
gateway({
6+
server: express(),
7+
8+
middlewares: [
9+
require('cors')(),
10+
require('helmet')()
11+
],
12+
13+
routes: [{
14+
prefix: '/public',
15+
target: 'http://localhost:3000',
16+
docs: {
17+
name: 'Public Service',
18+
endpoint: 'swagger.json',
19+
type: 'swagger'
20+
}
21+
}, {
22+
prefix: '/admin',
23+
target: 'http://localhost:3001',
24+
middlewares: [
25+
require('express-jwt')({
26+
secret: 'shhhhhhared-secret'
27+
})
28+
]
29+
}]
30+
}).listen(PORT, () => {
31+
console.log(`API Gateway listening on ${PORT} port!`)
32+
})
33+
34+
const service1 = require('restana')({})
35+
service1
36+
.get('/hi', (req, res) => res.send('Hello World!'))
37+
.start(3000).then(() => console.log('Public service listening on 3000 port!'))
38+
39+
const service2 = require('restana')({})
40+
service2
41+
.get('/users', (req, res) => res.send([]))
42+
.start(3001).then(() => console.log('Admin service listening on 3001 port!'))

index.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1+
/* eslint-disable no-useless-call */
2+
13
const fastProxy = require('fast-proxy')
24
const restana = require('restana')
35
const pump = require('pump')
46
const toArray = require('stream-to-array')
57
const defaultProxyHandler = (req, res, url, proxy, proxyOpts) => proxy(req, res, url, proxyOpts)
68
const DEFAULT_METHODS = require('restana/libs/methods')
9+
const send = require('@polka/send-type')
710

811
const gateway = (opts) => {
912
opts = Object.assign({
1013
middlewares: [],
1114
pathRegex: '/*'
1215
}, opts)
1316

14-
const server = (opts.restana instanceof Function) ? opts.restana() : restana(opts.restana || {
17+
const server = opts.server || ((opts.restana instanceof Function) ? opts.restana() : restana(opts.restana || {
1518
disableResponseEvent: true
16-
})
19+
}))
1720

1821
// registering global middlewares
1922
opts.middlewares.forEach(middleware => {
@@ -26,7 +29,7 @@ const gateway = (opts) => {
2629
docs: route.docs
2730
}))
2831
server.get('/services.json', (req, res) => {
29-
res.send(services)
32+
send(res, 200, services)
3033
})
3134

3235
// processing routes
@@ -40,6 +43,9 @@ const gateway = (opts) => {
4043
route.hooks.onRequest = route.hooks.onRequest || onRequestNoOp
4144
route.hooks.onResponse = route.hooks.onResponse || onResponse
4245

46+
// populating route middlewares
47+
route.middlewares = route.middlewares || []
48+
4349
// populating pathRegex if missing
4450
route.pathRegex = undefined === route.pathRegex ? opts.pathRegex : String(route.pathRegex)
4551

@@ -62,14 +68,14 @@ const gateway = (opts) => {
6268
method = method.toLowerCase()
6369

6470
if (server[method]) {
65-
server[method](
66-
// path
71+
server[method].apply(server, [
72+
// path
6773
route.prefix + route.pathRegex,
68-
// route handler
69-
handler(route, proxy, proxyHandler),
7074
// route middlewares
71-
route.middlewares
72-
)
75+
...route.middlewares,
76+
// route handler
77+
handler(route, proxy, proxyHandler)
78+
])
7379
}
7480
})
7581
})
@@ -100,7 +106,8 @@ const onResponse = async (req, res, stream) => {
100106
res.statusCode = stream.statusCode
101107
res.end(resBuffer)
102108
} catch (err) {
103-
res.send(err)
109+
res.statusCode = 500
110+
res.end(err.message)
104111
}
105112
} else {
106113
res.statusCode = stream.statusCode

0 commit comments

Comments
 (0)