Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error running with express and nextjs - TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Promise #61

Open
dominathan opened this issue Jan 14, 2021 · 6 comments
Labels
bug Something isn't working

Comments

@dominathan
Copy link

I'm using express with nextjs, but pulled the express app out. Code look llike so

// server.js 
const next = require('next');
const { initializeExpressWithNext } = require('./src/server/app');
const dev = process.env.NODE_ENV !== 'production';
const nextApplication = next({ dev });
const handle = nextApplication.getRequestHandler();

nextApplication.prepare().then(() => {
  const app = initializeExpressWithNext(handle);

  app.listen(3000, (err) => {
    if (err) throw err;
    console.log('Ready on http://localhost:3000');
  });
});


//app.js
const express = require('express');
const cors = require('cors');
const apiMetrics = require('prometheus-api-metrics');

const questionnaireRoutes = require('./routes/questionnaire');
const recaptchaRoutes = require('./routes/recaptcha');

/**
 * @function initializeExpressWithNext
 * @description to run this with nextjs, we need to pass in a callbackhandler
 * for all routes that are not defined by express, we do this by wrapping express
 * in a function to initialize the callback and runtime
 * @param {function} callbackHandler - nextjs server callbackhandler
 * @return {*} express application
 */
function initializeExpressWithNext(callbackHandler) {
  const app = express();
  app.use(apiMetrics());

  app.get('/healthcheck', cors(), (_, res) => {
    res.status(200).send();
  });

  app.use('/', questionnaireRoutes);
  app.use('/', recaptchaRoutes);

  app.get('*', (req, res) => {
    return callbackHandler(req, res);
  });

  return app;
}

module.exports = { initializeExpressWithNext };

The app starts fine, but when i navigate to /metrics or metrics.json, I get the following error

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Promise
    at ServerResponse.end (_http_outgoing.js:784:13)
    at ExpressMiddleware.middleware (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/prometheus-api-metrics/src/express-middleware.js:99:24)
    at Layer.handle [as handle_request] (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:317:13)
    at /Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:275:10)
    at expressInit (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/middleware/init.js:40:5)
    at Layer.handle [as handle_request] (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:317:13)
    at /Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:275:10)
    at query (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/middleware/query.js:45:5)
    at Layer.handle [as handle_request] (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/nathanhall/bwell/medstar-squeeze-page/node_modules/express/lib/router/index.js:317:13)
    ```
    
    I'll try to see if I can figure it out and make. PR but I'm on a tight timeline
@nautilor
Copy link

same problem here using express only, also calling /metrics.json returns always an empty json

@idanto
Copy link
Contributor

idanto commented Jan 31, 2021

This is related to a change in prom-client 13.x

@dominathan please fix the failed tests so we can approve the MR and release a major version

@idanto idanto added the bug Something isn't working label Jan 31, 2021
@nautilor
Copy link

nautilor commented Feb 1, 2021

Yeah I noticed that by using prom client only and await it it works fine

@dominathan
Copy link
Author

Not super familiar with sinon and async await. When I have more time I'll try to get the test to pass but if you have better ideas feel to fix it.

@kobik
Copy link
Collaborator

kobik commented Feb 9, 2021

@dominathan , please let me know if you need any help with that

@dominathan
Copy link
Author

I would please, I don't have any quick solution to fix the tests for different node versions. Currently just using prom-client directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

4 participants