Routing-controllers: Global middleware usage fails

Created on 25 Jan 2017  路  4Comments  路  Source: typestack/routing-controllers

Hello,

I am intresting on this project, and I was downloaded your example project (https://github.com/pleerock/routing-controllers-express-demo)

I would like to use Helmet ( https://helmetjs.github.io/ ) with global middleware declaration, but it not works, only works in middleware per-controller, or middleware per-action!

Please check my code:
Working code with middleware per-controller:
let helmet = require("helmet"); @UseBefore(helmet()) export class PostController {....}
but in main app.ts I would like to use:
let helmet = require("helmet"); expressApp.use(helmet());

And after I called some URL, then I got this Error:

Error: Can't set headers after they are sent.
at ServerResponse.setHeader (_http_outgoing.js:367:11)
at dnsPrefetchControl (E:workspaceadvnoderouting-controllers-express-demonode_modulesdns-prefetch-controlindex.js:9:11)
at call (E:workspaceadvnoderouting-controllers-express-demonode_modulesconnectindex.js:239:7)
at next (E:workspaceadvnoderouting-controllers-express-demonode_modulesconnectindex.js:183:5)
at Function.handle (E:workspaceadvnoderouting-controllers-express-demonode_modulesconnectindex.js:186:3)
at app (E:workspaceadvnoderouting-controllers-express-demonode_modulesconnectindex.js:51:37)
at Layer.handle [as handle_request] (E:workspaceadvnoderouting-controllers-express-demonode_modulesexpresslibrouterlayer.js:95:5)
at trim_prefix (E:workspaceadvnoderouting-controllers-express-demonode_modulesexpresslibrouterindex.js:312:13)
at E:workspaceadvnoderouting-controllers-express-demonode_modulesexpresslibrouterindex.js:280:7
at Function.process_params (E:workspaceadvnoderouting-controllers-express-demonode_modulesexpresslibrouterindex.js:330:12)

Used versions is the lates one in every dependency!

Can you help me what is the problem?

Most helpful comment

Maybe too late to answer the question :), You can just create a routing-controller compatible, like:

`import helmet from 'helmet';
import { ExpressMiddlewareInterface, Middleware } from 'routing-controllers';

@Middleware({ type: 'before' })
export class HelmetMiddleware implements ExpressMiddlewareInterface {
public use(request: any, response: any, next?: (err?: any) => any): any {
return helmet()(request, response, next);
}
}`

All 4 comments

Maybe you should use useExpressApp() and configure the middleware like in a normal express before?

@szabobar as @19majkel94 said you should configure your express instance and send that to useExpressApp and it should solve your problem. Reopen this issue and provide more details if you have problems.

Maybe too late to answer the question :), You can just create a routing-controller compatible, like:

`import helmet from 'helmet';
import { ExpressMiddlewareInterface, Middleware } from 'routing-controllers';

@Middleware({ type: 'before' })
export class HelmetMiddleware implements ExpressMiddlewareInterface {
public use(request: any, response: any, next?: (err?: any) => any): any {
return helmet()(request, response, next);
}
}`

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

codedoge picture codedoge  路  6Comments

xujif picture xujif  路  6Comments

kkorus picture kkorus  路  4Comments

AleskiWeb picture AleskiWeb  路  4Comments

circy picture circy  路  3Comments