# Add custom middleware to functions

**URL:** <https://forum.serverless.com/t/add-custom-middleware-to-functions/2662>\
**Category:** Serverless Architectures\
**Created:** [September 8, 2017, 10:09pm UTC](https://forum.serverless.com/t/add-custom-middleware-to-functions/2662 "2017-09-08T22:09:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhlsthrm](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/rhlsthrm/32/472_2.png) [@rhlsthrm](https://forum.serverless.com/u/rhlsthrm)\
**Post date:** [September 8, 2017, 10:09pm UTC](https://forum.serverless.com/t/add-custom-middleware-to-functions/2662/1 "2017-09-08T22:09:06Z")

</div>

I would like to add custom middleware to do things like count API requests and other analytics type things. Is there a way to do this in Serverless. Ideally it would be similar to setting up a Custom Authorizer, but able to work for any function.

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [September 9, 2017, 12:46pm UTC](https://forum.serverless.com/t/add-custom-middleware-to-functions/2662/2 "2017-09-09T12:46:11Z")

</div>

Before you spend too much time building metrics into your Lambda’s have a look at CloudWatch.

Lambda doesn’t really have the concept of middleware. If you need more then export a new function that implements the middleware and have it call your old function.

```javascript
oldHandler = (event, context, callback) => {
  // Your original handler here
}

exports.newHandler = (event, context, callback) => {
  // Start tracking
  oldHandler(event, context, (err, success) => {
    // Log here
    callback(err, success);
  });
}

```
