Skip to content

Use in Express middleware #35

@FeelHippo

Description

@FeelHippo

Hi there,

first off, thank you for this package, it's come in handy several times and always does a great job.
Documentation, however, could be expanded, and I have a question regarding its use in an ExpressJs middleware.

I would like to use "blocked" to investigate the cause of some timeouts we are experiencing in a controller.

I was wondering if I could do something like:

  • create a middleware like:
import { Request, Response, NextFunction } from 'express';
import * as blocked from 'blocked-at';

export default (_req: Request, _res: Response, next: NextFunction): void => {
  blocked(
    (time: any, stack: any) => {
      console.log(`Blocked for ${time}ms, operation started here:`, { stack });
    },
    { threshold: 1000 },
  );
  next();
};
  • and append this to our route handler
import { Router } from 'express';
import * as RestController from '.';
import ApiKeyAuth from '../../authorization/api-key-auth';
import blocked from '../../middleware/blocked';

router.post('/users', ApiKeyAuth, blocked, RestController.createUser);

I am testing this out with jMeter right now, and I have contrasting feelings on the effectiveness, what are your thoughts on this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions