From ef1b95d385d3e62a54ac454f855b8df34bcd42ce Mon Sep 17 00:00:00 2001 From: Rolando Santamaria Maso Date: Sat, 30 Jan 2021 18:35:00 +0100 Subject: [PATCH 1/4] adding url rewrite demo --- demos/url-rewrite.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 demos/url-rewrite.js diff --git a/demos/url-rewrite.js b/demos/url-rewrite.js new file mode 100644 index 0000000..6279af9 --- /dev/null +++ b/demos/url-rewrite.js @@ -0,0 +1,20 @@ +'use strict' + +const gateway = require('../index') +const PORT = process.env.PORT || 8080 + +gateway({ + routes: [{ + pathRegex: '', + prefix: '/customers/:customerId', + target: 'http://localhost:3000', + urlRewrite: ({ params: { customerId } }) => `/users/${customerId}` + }] +}).start(PORT).then(server => { + console.log(`API Gateway listening on ${PORT} port!`) +}) + +const service = require('restana')({}) +service + .get('/users/:id', (req, res) => res.send('Hello ' + req.params.id)) + .start(3000).then(() => console.log('Service listening on 3000 port!')) From acc58551d5c22edab2ee4b236770eb7128dbc05e Mon Sep 17 00:00:00 2001 From: Rolando Santamaria Maso Date: Sat, 30 Jan 2021 18:35:08 +0100 Subject: [PATCH 2/4] supporting url rewrite --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ef6f665..f0e243d 100644 --- a/index.js +++ b/index.js @@ -91,7 +91,9 @@ const gateway = (opts) => { const handler = (route, proxy, proxyHandler) => async (req, res, next) => { try { - req.url = req.url.replace(route.prefix, route.prefixRewrite) + req.url = route.urlRewrite + ? route.urlRewrite(req) + : req.url.replace(route.prefix, route.prefixRewrite) const shouldAbortProxy = await route.hooks.onRequest(req, res) if (!shouldAbortProxy) { const proxyOpts = Object.assign({ From f5cebf8f4a29b3e45d37e74574bd3faf7d189a9f Mon Sep 17 00:00:00 2001 From: Rolando Santamaria Maso Date: Tue, 2 Feb 2021 19:12:34 +0100 Subject: [PATCH 3/4] adding test cases --- test/config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/config.js b/test/config.js index 14caaa2..41ec4ad 100644 --- a/test/config.js +++ b/test/config.js @@ -69,7 +69,7 @@ module.exports = async () => { { pathRegex: '', prefix: '/endpoint-proxy-methods', - prefixRewrite: '/endpoint-proxy-methods', + urlRewrite: (req) => '/endpoint-proxy-methods', target: 'http://localhost:3000', methods: ['GET', 'POST'] }, From f641208f8ba3f06d875744b9cd7a28f6d6c983ea Mon Sep 17 00:00:00 2001 From: Rolando Santamaria Maso Date: Tue, 2 Feb 2021 19:16:50 +0100 Subject: [PATCH 4/4] adding urlRewrite hook documentation --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index d62856c..38c7aed 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,8 @@ module.exports.handler = serverless(service) }, // Optional "prefix rewrite" before request is forwarded. Default value: '' prefixRewrite: '', + // Optional "url rewrite" hook. If defined, the prefixRewrite setting is ignored. + urlRewrite: (req) => req.url, // Remote HTTP server URL to forward the request. // If proxyType = 'lambda', the value is the name of the Lambda function, version, or alias. target: 'http://localhost:3000',