1- var express = require ( ' express' ) ;
2- var bodyParser = require ( ' body-parser' ) ;
3- var expressLogging = require ( ' express-logging' ) ;
4- var queryString = require ( ' querystring' ) ;
5- var path = require ( ' path' ) ;
6- var conf = require ( ' ./config' ) ;
7- var jwtDecode = require ( ' jwt-decode' ) ;
1+ var express = require ( " express" ) ;
2+ var bodyParser = require ( " body-parser" ) ;
3+ var expressLogging = require ( " express-logging" ) ;
4+ var queryString = require ( " querystring" ) ;
5+ var path = require ( " path" ) ;
6+ var conf = require ( " ./config" ) ;
7+ var jwtDecode = require ( " jwt-decode" ) ;
88
99function handleErr ( err , response ) {
1010 response . statusCode = 500 ;
11- response . write ( ' Function invocation failed: ' + err . toString ( ) ) ;
11+ response . write ( " Function invocation failed: " + err . toString ( ) ) ;
1212 response . end ( ) ;
13- console . log ( ' Error during invocation: ' , err ) ;
13+ console . log ( " Error during invocation: " , err ) ;
1414 return ;
1515}
1616
@@ -40,12 +40,12 @@ function createCallback(response) {
4040 if ( lambdaResponse . body ) {
4141 response . write (
4242 lambdaResponse . isBase64Encoded
43- ? Buffer . from ( lambdaResponse . body , ' base64' )
43+ ? Buffer . from ( lambdaResponse . body , " base64" )
4444 : lambdaResponse . body
4545 ) ;
4646 } else {
4747 if (
48- process . env . CONTEXT !== ' production' ||
48+ process . env . CONTEXT !== " production" ||
4949 ! process . env . SILENCE_EMPTY_LAMBDA_WARNING
5050 )
5151 console . log (
@@ -59,8 +59,8 @@ function createCallback(response) {
5959
6060function promiseCallback ( promise , callback ) {
6161 if ( ! promise ) return ;
62- if ( typeof promise . then !== ' function' ) return ;
63- if ( typeof callback !== ' function' ) return ;
62+ if ( typeof promise . then !== " function" ) return ;
63+ if ( typeof callback !== " function" ) return ;
6464
6565 return promise . then (
6666 function ( data ) {
@@ -74,16 +74,16 @@ function promiseCallback(promise, callback) {
7474
7575function buildClientContext ( headers ) {
7676 // inject a client context based on auth header https://github.com/netlify/netlify-lambda/pull/57
77- if ( ! headers [ ' authorization' ] ) return ;
77+ if ( ! headers [ " authorization" ] ) return ;
7878
79- const parts = headers [ ' authorization' ] . split ( ' ' ) ;
80- if ( parts . length !== 2 || parts [ 0 ] !== ' Bearer' ) return ;
79+ const parts = headers [ " authorization" ] . split ( " " ) ;
80+ if ( parts . length !== 2 || parts [ 0 ] !== " Bearer" ) return ;
8181
8282 try {
8383 return {
8484 identity : {
85- url : ' NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_URL' ,
86- token : ' NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_TOKEN'
85+ url : " NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_URL" ,
86+ token : " NETLIFY_LAMBDA_LOCALLY_EMULATED_IDENTITY_TOKEN"
8787 } ,
8888 user : jwtDecode ( parts [ 1 ] )
8989 } ;
@@ -95,11 +95,21 @@ function buildClientContext(headers) {
9595function createHandler ( dir , static , timeout ) {
9696 return function ( request , response ) {
9797 // handle proxies without path re-writes (http-servr)
98- var cleanPath = request . path . replace ( / ^ \/ .n e t l i f y \/ f u n c t i o n s / , '' ) ;
99-
100- var func = cleanPath . split ( '/' ) . filter ( function ( e ) {
101- return e ;
102- } ) [ 0 ] ;
98+ var cleanPath = request . path . replace ( / ^ \/ .n e t l i f y \/ f u n c t i o n s / , "" ) ;
99+
100+ var func = cleanPath . split ( "/" ) . filter ( e => ! ! e ) [ 0 ] ;
101+ if ( typeof func === "undefined" ) {
102+ console . error (
103+ `Something went wrong and the function path derived from ${ cleanPath } (raw form: ${
104+ request . path
105+ } ) was undefined. Please doublecheck your function naming and toml configuration.`
106+ ) ;
107+ }
108+ if ( typeof dir === "undefined" ) {
109+ console . error (
110+ `Something went wrong and the function directory ${ dir } was undefined. Please doublecheck your toml configuration.`
111+ ) ;
112+ }
103113 var module = path . join ( process . cwd ( ) , dir , func ) ;
104114 if ( static ) {
105115 delete require . cache [ require . resolve ( module ) ] ;
@@ -114,7 +124,7 @@ function createHandler(dir, static, timeout) {
114124
115125 var isBase64 =
116126 request . body &&
117- ! ( request . headers [ ' content-type' ] || '' ) . match (
127+ ! ( request . headers [ " content-type" ] || "" ) . match (
118128 / t e x t | a p p l i c a t i o n | m u l t i p a r t \/ f o r m - d a t a /
119129 ) ;
120130 var lambdaRequest = {
@@ -123,7 +133,7 @@ function createHandler(dir, static, timeout) {
123133 queryStringParameters : queryString . parse ( request . url . split ( / \? ( .+ ) / ) [ 1 ] ) ,
124134 headers : request . headers ,
125135 body : isBase64
126- ? Buffer . from ( request . body . toString ( ) , ' utf8' ) . toString ( ' base64' )
136+ ? Buffer . from ( request . body . toString ( ) , " utf8" ) . toString ( " base64" )
127137 : request . body ,
128138 isBase64Encoded : isBase64
129139 } ;
@@ -163,22 +173,29 @@ exports.listen = function(port, static, timeout) {
163173 var config = conf . load ( ) ;
164174 var app = express ( ) ;
165175 var dir = config . build . functions || config . build . Functions ;
166- app . use ( bodyParser . raw ( { limit : ' 6mb' } ) ) ;
167- app . use ( bodyParser . text ( { limit : ' 6mb' , type : ' */*' } ) ) ;
176+ app . use ( bodyParser . raw ( { limit : " 6mb" } ) ) ;
177+ app . use ( bodyParser . text ( { limit : " 6mb" , type : " */*" } ) ) ;
168178 app . use (
169179 expressLogging ( console , {
170- blacklist : [ ' /favicon.ico' ]
180+ blacklist : [ " /favicon.ico" ]
171181 } )
172182 ) ;
173183
174- app . get ( ' /favicon.ico' , function ( req , res ) {
184+ app . get ( " /favicon.ico" , function ( req , res ) {
175185 res . status ( 204 ) . end ( ) ;
176186 } ) ;
177- app . all ( '*' , createHandler ( dir , static , timeout ) ) ;
187+ app . get ( "/" , function ( req , res ) {
188+ res
189+ . status ( 404 )
190+ . send (
191+ `You have requested the root of http://localhost:${ port } . This is likely a mistake. netlify-lambda serves functions at htttp://localhost:${ port } /.netlify/functions/your-function-name, please fix your code.`
192+ ) ;
193+ } ) ;
194+ app . all ( "*" , createHandler ( dir , static , timeout ) ) ;
178195
179196 app . listen ( port , function ( err ) {
180197 if ( err ) {
181- console . error ( ' Unable to start lambda server: ' , err ) ;
198+ console . error ( " Unable to start lambda server: " , err ) ;
182199 process . exit ( 1 ) ;
183200 }
184201
0 commit comments