Skip to content

Commit e57fb71

Browse files
committed
become framework agnostic
1 parent 8711475 commit e57fb71

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
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: []

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)