Skip to content

Commit 261be29

Browse files
committed
Added --debug option, log STDOUT
Moved port # to constant
1 parent f2912f0 commit 261be29

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

server.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ const fs = require('fs');
1616
const http = require('http');
1717
const url = require('url');
1818

19+
const SERVER_PORT = 3000;
20+
1921
// Process CLI options.
2022
const program = new Command();
2123

2224
program
2325
.usage('[options]')
2426

2527
.option('--handler <path>', 'Lambda@Edge handler script.')
26-
.option('--port <number>', 'HTTP server port number.', 3000)
28+
.option('--port <number>', 'HTTP server port number.', SERVER_PORT)
29+
.option('--debug <boolean>', 'Log event to STDOUT.', false)
2730

2831
.action(function(opts) {
2932
const errors = [];
@@ -67,7 +70,7 @@ program
6770
if (process.env.NODE_ENV === 'test') {
6871
const {handler} = require(process.env.TEST_HANDLER);
6972

70-
module.exports = initServer(handler, 3000);
73+
module.exports = initServer(handler, SERVER_PORT);
7174
} else {
7275
program.parse();
7376
}
@@ -87,13 +90,16 @@ if (process.env.NODE_ENV === 'test') {
8790
*/
8891
function initServer(handler, port) {
8992
return http.createServer(function(req, res) {
90-
let body;
93+
let body = '';
9194

9295
req.on('data', function(data) {
9396
body += data;
9497
});
9598

9699
req.on('end', function() {
100+
const path = url.parse(req.url).pathname;
101+
const query = url.parse(req.url).query;
102+
97103
if (body) {
98104
body = encodeBody(body);
99105
}
@@ -107,8 +113,8 @@ function initServer(handler, port) {
107113
clientIp: res.socket.remoteAddress,
108114
headers: formatHeaders(req.headers).toEdge(),
109115
method: req.method,
110-
querystring: url.parse(req.url).query,
111-
uri: url.parse(req.url).pathname,
116+
querystring: query,
117+
uri: path,
112118
body: {
113119
data: body
114120
}
@@ -154,6 +160,9 @@ function initServer(handler, port) {
154160
// Synchronous handling.
155161
handler(event, null, callback);
156162
}
163+
164+
console.log(req.method, path, JSON.stringify(event));
165+
157166
} catch (err) {
158167
this.emit('error', Error('Malformed handler method. Exiting..'));
159168
}

0 commit comments

Comments
 (0)