@@ -16,13 +16,6 @@ if (parsedArgs.watch) {
1616
1717const server = http . createServer ( ( req , res ) => {
1818 readRequestBodyAsJson ( req , bodyJson => {
19- const resolvedPath = path . resolve ( process . cwd ( ) , bodyJson . moduleName ) ;
20- const invokedModule = dynamicRequire ( resolvedPath ) ;
21- const func = bodyJson . exportedFunctionName ? invokedModule [ bodyJson . exportedFunctionName ] : invokedModule ;
22- if ( ! func ) {
23- throw new Error ( 'The module "' + resolvedPath + '" has no export named "' + bodyJson . exportedFunctionName + '"' ) ;
24- }
25-
2619 let hasSentResult = false ;
2720 const callback = ( errorValue , successValue ) => {
2821 if ( ! hasSentResult ) {
@@ -31,9 +24,9 @@ const server = http.createServer((req, res) => {
3124 res . statusCode = 500 ;
3225
3326 if ( errorValue . stack ) {
34- res . end ( errorValue . stack ) ;
27+ res . end ( errorValue . stack ) ;
3528 } else {
36- res . end ( errorValue . toString ( ) ) ;
29+ res . end ( errorValue . toString ( ) ) ;
3730 }
3831 } else if ( typeof successValue !== 'string' ) {
3932 // Arbitrary object/number/etc - JSON-serialize it
@@ -61,6 +54,13 @@ const server = http.createServer((req, res) => {
6154 } ) ;
6255
6356 try {
57+ const resolvedPath = path . resolve ( process . cwd ( ) , bodyJson . moduleName ) ;
58+ const invokedModule = dynamicRequire ( resolvedPath ) ;
59+ const func = bodyJson . exportedFunctionName ? invokedModule [ bodyJson . exportedFunctionName ] : invokedModule ;
60+ if ( ! func ) {
61+ throw new Error ( 'The module "' + resolvedPath + '" has no export named "' + bodyJson . exportedFunctionName + '"' ) ;
62+ }
63+
6464 func . apply ( null , [ callback ] . concat ( bodyJson . args ) ) ;
6565 } catch ( synchronousException ) {
6666 callback ( synchronousException , null ) ;
0 commit comments