Error: glx.serveApp(app) expects a node/express app in the format `function (req, res) { ... } #53

Closed
opened 2020-06-16 07:30:56 +00:00 by Ghost · 5 comments

Hey,

I am trying to convert an existing express app to use greenlock instead of AWS ALB + CertManager. I was able to go through the quickstart successfully but when I try to mount my existing app I get this error:

(node:21010) UnhandledPromiseRejectionWarning: Error: glx.serveApp(app) expects a node/express app in the format `function (req, res) { ... }`
    at /home/ubuntu/kb/node_modules/@root/greenlock-express/servers.js:94:21
    at new Promise (<anonymous>)
    at Object.servers.serveApp (/home/ubuntu/kb/node_modules/@root/greenlock-express/servers.js:91:16)
    at /home/ubuntu/kb/node_modules/@root/greenlock-express/single.js:31:21
    at Object.ready (/home/ubuntu/kb/node_modules/@root/greenlock-express/single.js:15:13)
    at Object.serve (/home/ubuntu/kb/node_modules/@root/greenlock-express/single.js:30:20)
    at Object.<anonymous> (/home/ubuntu/kb/server/server.js:18:4)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
(node:21010) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:21010) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

This is on express 4.17.1.
The express app is fairly complex with routers and custom middleware but nothing "hacky". It is written in typescript so maybe that is an issue?

The greenlock server code is basically the same as in the quickstart

var app = require('./dist/server.js');

require('greenlock-express')
  .init({
    packageRoot: __dirname,
    configDir: './greenlock.d',

    // contact for security and critical bug notices
    maintainerEmail: 'valentin@elev.io',

    // whether or not to run at cloudscale
    cluster: false,
  })
  // Serves on 80 and 443
  // Get's SSL certificates magically!
  .serve(app);

I cannot post the full server code here, if there is anything in particular that I need to check I can do that. Overall I am not sure how a working express app wouldn't conform to what greenlock expects.

Hey, I am trying to convert an existing express app to use greenlock instead of AWS ALB + CertManager. I was able to go through the quickstart successfully but when I try to mount my existing app I get this error: ``` (node:21010) UnhandledPromiseRejectionWarning: Error: glx.serveApp(app) expects a node/express app in the format `function (req, res) { ... }` at /home/ubuntu/kb/node_modules/@root/greenlock-express/servers.js:94:21 at new Promise (<anonymous>) at Object.servers.serveApp (/home/ubuntu/kb/node_modules/@root/greenlock-express/servers.js:91:16) at /home/ubuntu/kb/node_modules/@root/greenlock-express/single.js:31:21 at Object.ready (/home/ubuntu/kb/node_modules/@root/greenlock-express/single.js:15:13) at Object.serve (/home/ubuntu/kb/node_modules/@root/greenlock-express/single.js:30:20) at Object.<anonymous> (/home/ubuntu/kb/server/server.js:18:4) at Module._compile (internal/modules/cjs/loader.js:1138:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47 (node:21010) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:21010) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. ``` This is on express `4.17.1`. The express app is fairly complex with routers and custom middleware but nothing "hacky". It is written in typescript so maybe that is an issue? The greenlock server code is basically the same as in the quickstart ``` var app = require('./dist/server.js'); require('greenlock-express') .init({ packageRoot: __dirname, configDir: './greenlock.d', // contact for security and critical bug notices maintainerEmail: 'valentin@elev.io', // whether or not to run at cloudscale cluster: false, }) // Serves on 80 and 443 // Get's SSL certificates magically! .serve(app); ``` I cannot post the full server code here, if there is anything in particular that I need to check I can do that. Overall I am not sure how a working express app wouldn't conform to what greenlock expects.
Owner

You're not doing what you think you're doing - which is why it's telling you.

Try editing this part:

.serveApp(function (req, res) {
  console.log("I should expect this to work?", app && 'function' === typeof app && 2 === app.length);
  app(req, res);
});
You're not doing what you think you're doing - which is why it's telling you. Try editing this part: ```js .serveApp(function (req, res) { console.log("I should expect this to work?", app && 'function' === typeof app && 2 === app.length); app(req, res); }); ```
Owner

If you're not using JavaScript you may have to run babel or some such. My understanding is that .mjs / ES20xx doesn't play well with actual JavaScript.

If you're not using JavaScript you may have to run babel or some such. My understanding is that `.mjs` / ES20xx doesn't play well with actual JavaScript.
Author

Thanks for the quick response!

I assume you meant .serve instead of .serveApp? .serveApp throws a "is not a function error".

I tried wrapping the app object and the server starts up successfully but am getting this error when the service is called:

I should expect this to work? false
/home/ubuntu/kb/server/server.js:20
  app(req, res);
  ^

TypeError: app is not a function
    at /home/ubuntu/kb/server/server.js:20:3
    at /home/ubuntu/kb/node_modules/@root/greenlock-express/servers.js:79:17
    at realNext (/home/ubuntu/kb/node_modules/@root/greenlock-express/https-middleware.js:10:17)
    at Server.<anonymous> (/home/ubuntu/kb/node_modules/@root/greenlock-express/https-middleware.js:72:9)

I added some logging

console.log(typeof app) --> object
console.log(app.length) --> undefined

So the app object isn't a function, I don't have enough experience with express to know if this is odd? Setting up the server we don't do anything fancy.

const app = express();

and the a bunch of app.get() and app.use() to configure routes, middleware and child routers and module.exports = app; at the end.

Thanks for the quick response! I assume you meant `.serve` instead of `.serveApp`? `.serveApp` throws a "is not a function error". I tried wrapping the app object and the server starts up successfully but am getting this error when the service is called: ``` I should expect this to work? false /home/ubuntu/kb/server/server.js:20 app(req, res); ^ TypeError: app is not a function at /home/ubuntu/kb/server/server.js:20:3 at /home/ubuntu/kb/node_modules/@root/greenlock-express/servers.js:79:17 at realNext (/home/ubuntu/kb/node_modules/@root/greenlock-express/https-middleware.js:10:17) at Server.<anonymous> (/home/ubuntu/kb/node_modules/@root/greenlock-express/https-middleware.js:72:9) ``` I added some logging ``` console.log(typeof app) --> object console.log(app.length) --> undefined ``` So the app object isn't a function, I don't have enough experience with express to know if this is odd? Setting up the server we don't do anything fancy. ``` const app = express(); ``` and the a bunch of `app.get()` and `app.use()` to configure routes, middleware and child routers and `module.exports = app;` at the end.
Author

But yea maybe you are right, only running the typescript compiler might not be enough and we need to transpile more.

But yea maybe you are right, only running the typescript compiler might not be enough and we need to transpile more.
Author

Figured it out, the problem was with the export/import structure. The file that includes module.exports = app also included another export. Using require imported an object with both variables. Changing that made it work!

Figured it out, the problem was with the export/import structure. The file that includes `module.exports = app` also included another export. Using `require` imported an object with both variables. Changing that made it work!
Ghost closed this issue 2020-06-16 23:53:31 +00:00
Sign in to join this conversation.
No Label
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: root/greenlock-express.js#53
No description provided.