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

made example in readme runnable #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Connect / Restify middleware for CLS

Dirt-simple middleware for Connect and Restify handlers into a
continuation-local storage context.
Dirt-simple middleware for Connect and Restify handlers that sets up
a continuation-local storage context per request.

Example `app.js`:

Expand All @@ -11,25 +11,29 @@ var express = require('express');
var clsify = require('cls-middleware');
var route = require('./route.js');

var ns = cls.createNamespace('namespace');
// you can use any namespace name as long as you use the same for set/get
var ns = cls.createNamespace('requestScope');
var requestCounter = 0;

var app = express();
// let the middleware set up the continuation-local storage context
app.use(clsify(ns));

ns.set('whatever', 'a value');
app.use(function (req, res, next) {
// put something in the namespace, a requestId in this case
ns.set('reqId', ++requestCounter);
next();
});

app.get('/users', route);
```

with `./route.js`:
and `./route.js`:

```js
var cls = require('continuation-local-storage');

module.exports = function (req, res, next) {
// pulling from the namespace, and set up per request
res.send({value : cls.getNameSpace('namespace').get('whatever')});

next();
// pulling from the namespace which is set up per request
res.send({value : cls.getNamespace('requestScope').get('reqId')});
};
```