@@ -12,7 +12,10 @@ npm i fast-gateway
12
12
```
13
13
14
14
## Usage
15
- ### Gateway
15
+ Next we describe two examples proxying HTTP and Lambda downstream services.
16
+ > For simplicity of reading, both examples are separated, however a single gateway configuration supports all routes configurations.
17
+ ### HTTP Proxying
18
+ #### Gateway
16
19
``` js
17
20
const gateway = require (' fast-gateway' )
18
21
const server = gateway ({
@@ -24,14 +27,56 @@ const server = gateway({
24
27
25
28
server .start (8080 )
26
29
```
27
- ### Remote Service
30
+ #### Remote Service
28
31
``` js
29
32
const service = require (' restana' )()
30
33
service .get (' /get' , (req , res ) => res .send (' Hello World!' ))
31
34
32
35
service .start (3000 )
33
36
```
34
37
38
+ ### Lambda Proxying
39
+ #### Gateway
40
+ ``` bash
41
+ npm i http-lambda-proxy
42
+ ```
43
+ ``` js
44
+ const gateway = require (' fast-gateway' )
45
+ const server = gateway ({
46
+ routes: [{
47
+ prefix: ' /service' ,
48
+ target: ' my-lambda-serverless-api' ,
49
+ lambdaProxy: {
50
+ region: ' eu-central-1'
51
+ }
52
+ }]
53
+ })
54
+
55
+ server .start (8080 )
56
+ ```
57
+ #### Lambda Implementation
58
+ ``` js
59
+ const serverless = require (' serverless-http' )
60
+ const json = require (' serverless-json-parser' )
61
+ const query = require (' connect-query' )
62
+
63
+ const service = require (' restana' )()
64
+ service .use (query ())
65
+ service .use (json ())
66
+
67
+ // routes
68
+ service .get (' /get' , (req , res ) => {
69
+ res .send ({ msg: ' Go Serverless!' })
70
+ })
71
+ service .post (' /post' , (req , res ) => {
72
+ res .send (req .body )
73
+ })
74
+
75
+ // export handler
76
+ module .exports .handler = serverless (service)
77
+
78
+ ```
79
+
35
80
## Configuration options explained
36
81
``` js
37
82
{
@@ -42,7 +87,6 @@ service.start(3000)
42
87
// If omitted, restana is used as default HTTP framework
43
88
server,
44
89
// Optional restana library configuration (https://www.npmjs.com/package/restana#configuration)
45
- //
46
90
// Please note that if "server" is provided, this settings are ignored.
47
91
restana: {},
48
92
// Optional global middlewares in the format: (req, res, next) => next()
@@ -51,23 +95,36 @@ service.start(3000)
51
95
// Optional global value for routes "pathRegex". Default value: '/*'
52
96
pathRegex: ' /*' ,
53
97
// Optional global requests timeout value (given in milliseconds). Default value: '0' (DISABLED)
98
+ // Ignored if proxyType = 'lambda'
54
99
timeout: 0 ,
55
100
// Optional "target" value that overrides the routes "target" config value. Feature intended for testing purposes.
56
101
targetOverride: " https://yourdev.api-gateway.com" ,
57
102
58
103
// HTTP proxy
59
104
routes: [{
105
+ // Optional proxy type definition. Supported values: http, lambda
106
+ // Default value: http
107
+ proxyType: ' http'
60
108
// Optional `fast-proxy` library configuration (https://www.npmjs.com/package/fast-proxy#options)
61
109
// base parameter defined as the route target. Default value: {}
110
+ // This settings apply only when proxyType = 'http'
62
111
fastProxy: {},
112
+ // Optional `http-lambda-proxy` library configuration (https://www.npmjs.com/package/http-lambda-proxy#options)
113
+ // The 'target' parameter is extracted from route.target, default region = 'eu-central-1'
114
+ // This settings apply only when proxyType = 'lambda'
115
+ lambdaProxy: {
116
+ region: ' eu-central-1'
117
+ },
63
118
// Optional proxy handler function. Default value: (req, res, url, proxy, proxyOpts) => proxy(req, res, url, proxyOpts)
64
119
proxyHandler : () => {},
65
120
// Optional flag to indicate if target uses the HTTP2 protocol. Default value: false
121
+ // This setting apply only when proxyType = 'http'
66
122
http2: false ,
67
123
// Optional path matching regex. Default value: '/*'
68
124
// In order to disable the 'pathRegex' at all, you can use an empty string: ''
69
125
pathRegex: ' /*' ,
70
126
// Optional service requests timeout value (given in milliseconds). Default value: '0' (DISABLED)
127
+ // This setting apply only when proxyType = 'http'
71
128
timeout: 0 ,
72
129
// route prefix
73
130
prefix: ' /public' ,
@@ -79,7 +136,8 @@ service.start(3000)
79
136
},
80
137
// Optional "prefix rewrite" before request is forwarded. Default value: ''
81
138
prefixRewrite: ' ' ,
82
- // Remote HTTP server URL to forward the request
139
+ // Remote HTTP server URL to forward the request.
140
+ // If proxyType = 'lambda', the value is the name of the Lambda function, version, or alias.
83
141
target: ' http://localhost:3000' ,
84
142
// Optional HTTP methods to limit the requests proxy to certain verbs only
85
143
// Supported HTTP methods: ['GET', 'DELETE', 'PATCH', 'POST', 'PUT', 'HEAD', 'OPTIONS', 'TRACE']
@@ -99,54 +157,14 @@ service.start(3000)
99
157
// ...
100
158
}
101
159
102
- // other options allowed https://www.npmjs.com/package/fast-proxy#opts
160
+ // if proxyType= 'http', other options allowed https://www.npmjs.com/package/fast-proxy#opts
103
161
}
104
162
}]
105
163
}
106
164
```
107
- ### onResponse Hook default implementation
108
- For developers reference, next we describe how the default ` onResponse ` hook looks like:
109
- ``` js
110
- const pump = require (' pump' )
111
- const toArray = require (' stream-to-array' )
112
- const TRANSFER_ENCODING_HEADER_NAME = ' transfer-encoding'
113
-
114
- const onResponse = async (req , res , stream ) => {
115
- const chunked = stream .headers [TRANSFER_ENCODING_HEADER_NAME ]
116
- ? stream .headers [TRANSFER_ENCODING_HEADER_NAME ].endsWith (' chunked' )
117
- : false
118
-
119
- if (req .headers .connection === ' close' && chunked) {
120
- try {
121
- // remove transfer-encoding header
122
- const transferEncoding = stream .headers [TRANSFER_ENCODING_HEADER_NAME ].replace (/ (,( )? )? chunked/ , ' ' )
123
- if (transferEncoding) {
124
- res .setHeader (TRANSFER_ENCODING_HEADER_NAME , transferEncoding)
125
- } else {
126
- res .removeHeader (TRANSFER_ENCODING_HEADER_NAME )
127
- }
128
-
129
- if (! stream .headers [' content-length' ]) {
130
- // pack all pieces into 1 buffer to calculate content length
131
- const resBuffer = Buffer .concat (await toArray (stream))
132
-
133
- // add content-length header and send the merged response buffer
134
- res .setHeader (' content-length' , ' ' + Buffer .byteLength (resBuffer))
135
- res .statusCode = stream .statusCode
136
- res .end (resBuffer)
137
-
138
- return
139
- }
140
- } catch (err) {
141
- res .statusCode = 500
142
- res .end (err .message )
143
- }
144
- }
165
+ ### onResponse hooks default implementation
166
+ For developers reference, default hooks implementation are located in ` lib/default-hooks.js ` file.
145
167
146
- res .statusCode = stream .statusCode
147
- pump (stream, res)
148
- }
149
- ```
150
168
## The "* GET /services.json* " endpoint
151
169
Since version ` 1.3.5 ` the gateway exposes minimal documentation about registered services at: ` GET /services.json `
152
170
@@ -312,6 +330,7 @@ This is your repo ;)
312
330
## Related projects
313
331
- middleware-if-unless (https://www.npmjs.com/package/middleware-if-unless )
314
332
- fast-proxy (https://www.npmjs.com/package/fast-proxy )
333
+ - http-lambda-proxy (https://www.npmjs.com/package/http-lambda-proxy )
315
334
- restana (https://www.npmjs.com/package/restana )
316
335
317
336
## Benchmarks
0 commit comments