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" ) ;
8
8
9
9
function handleErr ( err , response ) {
10
10
response . statusCode = 500 ;
11
- response . write ( ' Function invocation failed: ' + err . toString ( ) ) ;
11
+ response . write ( " Function invocation failed: " + err . toString ( ) ) ;
12
12
response . end ( ) ;
13
- console . log ( ' Error during invocation: ' , err ) ;
13
+ console . log ( " Error during invocation: " , err ) ;
14
14
return ;
15
15
}
16
16
@@ -40,12 +40,12 @@ function createCallback(response) {
40
40
if ( lambdaResponse . body ) {
41
41
response . write (
42
42
lambdaResponse . isBase64Encoded
43
- ? Buffer . from ( lambdaResponse . body , ' base64' )
43
+ ? Buffer . from ( lambdaResponse . body , " base64" )
44
44
: lambdaResponse . body
45
45
) ;
46
46
} else {
47
47
if (
48
- process . env . CONTEXT !== ' production' ||
48
+ process . env . CONTEXT !== " production" ||
49
49
! process . env . SILENCE_EMPTY_LAMBDA_WARNING
50
50
)
51
51
console . log (
@@ -59,8 +59,8 @@ function createCallback(response) {
59
59
60
60
function promiseCallback ( promise , callback ) {
61
61
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 ;
64
64
65
65
return promise . then (
66
66
function ( data ) {
@@ -74,16 +74,16 @@ function promiseCallback(promise, callback) {
74
74
75
75
function buildClientContext ( headers ) {
76
76
// 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 ;
78
78
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 ;
81
81
82
82
try {
83
83
return {
84
84
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"
87
87
} ,
88
88
user : jwtDecode ( parts [ 1 ] )
89
89
} ;
@@ -95,11 +95,21 @@ function buildClientContext(headers) {
95
95
function createHandler ( dir , static , timeout ) {
96
96
return function ( request , response ) {
97
97
// 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
+ }
103
113
var module = path . join ( process . cwd ( ) , dir , func ) ;
104
114
if ( static ) {
105
115
delete require . cache [ require . resolve ( module ) ] ;
@@ -114,7 +124,7 @@ function createHandler(dir, static, timeout) {
114
124
115
125
var isBase64 =
116
126
request . body &&
117
- ! ( request . headers [ ' content-type' ] || '' ) . match (
127
+ ! ( request . headers [ " content-type" ] || "" ) . match (
118
128
/ 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 /
119
129
) ;
120
130
var lambdaRequest = {
@@ -123,7 +133,7 @@ function createHandler(dir, static, timeout) {
123
133
queryStringParameters : queryString . parse ( request . url . split ( / \? ( .+ ) / ) [ 1 ] ) ,
124
134
headers : request . headers ,
125
135
body : isBase64
126
- ? Buffer . from ( request . body . toString ( ) , ' utf8' ) . toString ( ' base64' )
136
+ ? Buffer . from ( request . body . toString ( ) , " utf8" ) . toString ( " base64" )
127
137
: request . body ,
128
138
isBase64Encoded : isBase64
129
139
} ;
@@ -163,22 +173,29 @@ exports.listen = function(port, static, timeout) {
163
173
var config = conf . load ( ) ;
164
174
var app = express ( ) ;
165
175
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 : " */*" } ) ) ;
168
178
app . use (
169
179
expressLogging ( console , {
170
- blacklist : [ ' /favicon.ico' ]
180
+ blacklist : [ " /favicon.ico" ]
171
181
} )
172
182
) ;
173
183
174
- app . get ( ' /favicon.ico' , function ( req , res ) {
184
+ app . get ( " /favicon.ico" , function ( req , res ) {
175
185
res . status ( 204 ) . end ( ) ;
176
186
} ) ;
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 ) ) ;
178
195
179
196
app . listen ( port , function ( err ) {
180
197
if ( err ) {
181
- console . error ( ' Unable to start lambda server: ' , err ) ;
198
+ console . error ( " Unable to start lambda server: " , err ) ;
182
199
process . exit ( 1 ) ;
183
200
}
184
201
0 commit comments