Skip to content

Commit 6ae5c32

Browse files
author
Iain Collins
committed
Add option to pass a proxy
If specified the --proxy option is used as a fallback route handler. This can be used to run lambdas and a website under the same hostname/port locally.
1 parent 1a48745 commit 6ae5c32

4 files changed

Lines changed: 352 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,5 @@ Here's is a list of the things that happen when `now-lambda` process your `now.j
8787
## CLI arguments
8888

8989
* `--config` - path to `now.json` file
90-
* `--port` - by default the local server listens on port 8004. You can change it via this argument.
90+
* `--port` - by default the local server listens on port 8004. You can change it via this argument.
91+
* `--proxy` - a URL to use as a fallback proxy for requests that would otherwise fail (e.g. `http://localhost:3000`)

‎index.js‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const yargs = require('yargs');
88
const NamedRegExp = require('named-regexp-groups');
99
const cors = require('cors');
1010
const extglob = require('extglob');
11-
const mime = require('mime-types')
11+
const mime = require('mime-types');
12+
const request = require('request');
1213

1314
const argv = yargs
1415
.options({
@@ -21,7 +22,12 @@ const argv = yargs
2122
type: 'number',
2223
default: 8004,
2324
desc: 'The port where the runner is listening for your requests'
24-
}
25+
},
26+
proxy: {
27+
type: 'string',
28+
default: null,
29+
desc: 'A URL to use as a fallback proxy for requests that would otherwise fail (e.g. "http://localhost:3000")'
30+
},
2531
})
2632
.help()
2733
.argv;
@@ -132,11 +138,23 @@ if (fs.existsSync(nowConfPath)) {
132138

133139
delete require.cache[require.resolve(handlerFilepath)];
134140
} else if (fs.existsSync(handlerFilepath)) {
135-
console.log(logMessage.join('\n'));
136-
sendFile(res, handlerFilepath);
141+
if (argv.proxy) {
142+
const proxy = request(`${argv.proxy}${req.url}`)
143+
req.pipe(proxy)
144+
proxy.pipe(res)
145+
} else {
146+
console.log(logMessage.join('\n'));
147+
sendFile(res, handlerFilepath);
148+
}
137149
} else {
138-
res.statusCode = 404;
139-
res.end(handlerFilepath + ' can not be found.');
150+
if (argv.proxy) {
151+
const proxy = request(`${argv.proxy}${req.url}`)
152+
req.pipe(proxy)
153+
proxy.pipe(res)
154+
} else {
155+
res.statusCode = 404;
156+
res.end(handlerFilepath + ' can not be found.');
157+
}
140158
}
141159
};
142160

@@ -158,4 +176,4 @@ if (fs.existsSync(nowConfPath)) {
158176
});
159177
} else {
160178
error('now.json can not be found in ' + nowConfPath);
161-
}
179+
}

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "now-lambda-runner",
3-
"version": "3.3.0",
3+
"version": "3.4.0",
44
"bin": {
55
"now-lambda": "./index.js"
66
},
@@ -28,6 +28,7 @@
2828
"extglob": "3.0.0",
2929
"mime-types": "2.1.21",
3030
"named-regexp-groups": "^1.0.3",
31+
"request": "^2.88.0",
3132
"yargs": "12.0.5"
3233
}
3334
}

0 commit comments

Comments
 (0)