@@ -26,7 +26,7 @@ program
26
26
27
27
. option ( '--handler <path>' , 'Lambda@Edge handler script.' )
28
28
. option ( '--port <number>' , 'HTTP server port number.' , SERVER_PORT )
29
- . option ( '--silent' , 'Disable logging events to STDOUT' )
29
+ . option ( '--silent' , 'Disable logging Router errors to STDOUT' , false )
30
30
31
31
. action ( function ( opts ) {
32
32
const errors = [ ] ;
@@ -84,14 +84,14 @@ if (process.env.NODE_ENV === 'test') {
84
84
* @param {Function } port
85
85
* HTTP server port number.
86
86
*
87
- * @param {Boolean } logEvents
88
- * Log events to STDOUT (default: true).
87
+ * @param {Boolean } logRouterError
88
+ * Log Router errors to STDOUT (default: true).
89
89
*
90
90
* @return {Object }
91
91
*
92
92
* @see https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html
93
93
*/
94
- function initServer ( handler , port , logEvents = true ) {
94
+ function initServer ( handler , port , logRouterError = true ) {
95
95
const server = http . createServer ( function ( req , res ) {
96
96
let body = '' ;
97
97
@@ -167,28 +167,21 @@ function initServer(handler, port, logEvents = true) {
167
167
log ( Date . now ( ) , req . method , path , JSON . stringify ( event ) ) ;
168
168
169
169
} catch ( err ) {
170
- this . emit ( 'error' , Error ( 'Malformed handler method. Exiting..' ) ) ;
170
+ logRouterError && this . emit ( 'error' , err ) ;
171
171
}
172
172
} ) ;
173
173
} ) ;
174
174
175
- // Log event to STDOUT.
176
- const log = function ( ) {
177
- if ( logEvents ) {
178
- console . log ( ...arguments ) ;
179
- }
180
- } ;
181
-
182
175
// Start HTTP server; increment port if used.
183
176
return server
184
177
. listen ( port , ( ) => {
185
- log ( `HTTP server started. Listening on port ${ port } ` ) ;
178
+ console . info ( `HTTP server started. Listening on port ${ port } ` ) ;
186
179
} )
187
180
. on ( 'error' , function ( err ) {
188
181
if ( err . code === 'EADDRINUSE' ) {
189
182
this . close ( ) ;
190
183
191
- log ( `Port ${ port } in use. Trying another port.` ) ;
184
+ console . error ( `Port ${ port } in use. Trying another port.` ) ;
192
185
193
186
initServer ( handler , port + 1 ) ;
194
187
}
0 commit comments