How do I add an express middleware function to a swagger node project ?
In case I want to do some custom error handling in code , I am writing a middleware like this,
app.use(function(err, req, res, next) {
console.error("inside error middleware");
res.status(500).send('Something broke!'); });
The function is not behaving as I expect an express middleware to work.What is the best way to add middlewares to swagger-node projects.
Solved! Go to Solution.
@Hemanth Shetty and @Srinandan Sridhar, have you considered swagger-tools ? I use it extensively on my projects and runs pretty smoothly. The definition of routes is slightly than in swagger-node. Both great frameworks. Sorry that it doesn't address the question directly.
Here's a snippet of my code handling errors:
// Validate Swagger requests app.use(middleware.swaggerValidator()); // Route validated requests to appropriate controller app.use(middleware.swaggerRouter(options)); // error handler // no stacktraces leaked to user unless in development environment app.use(function (err, req, res, next) { "use strict"; utils.sendError(err.status || err.code || 500, err, req, res); });
Hope it helps!
@kevinswiber or @Scott Ganyo - can either of you please help?
I don't think I have enough information because I just tried doing that and it worked fine.
One possible gotcha: Be sure you call next() in your controllers.
@Hemanth Shetty and @Srinandan Sridhar, have you considered swagger-tools ? I use it extensively on my projects and runs pretty smoothly. The definition of routes is slightly than in swagger-node. Both great frameworks. Sorry that it doesn't address the question directly.
Here's a snippet of my code handling errors:
// Validate Swagger requests app.use(middleware.swaggerValidator()); // Route validated requests to appropriate controller app.use(middleware.swaggerRouter(options)); // error handler // no stacktraces leaked to user unless in development environment app.use(function (err, req, res, next) { "use strict"; utils.sendError(err.status || err.code || 500, err, req, res); });
Hope it helps!
User | Count |
---|---|
2 | |
1 | |
1 | |
1 | |
1 |