DEV Community

Discussion on: Simplify your Node code with Continuation Local Storage variables

Collapse
renoirb profile image
Renoir Boulanger • Edited on

Bookmarking.
I wonder the use-case and usage context.

At first glance it looks like something related to HTTP server middleware, and/or at leadt Node.js runtime side.

If that's the case, in KoaJS, there's ctx.state, and Express has locals and some way of abstracting/storing state such as shoring in a cookie or external, like koa-session does.

Before (ECMAScript 5) we had to use magic names and enumeration, then we got defineProperties. There are a few implementation of that idea. Now that Proxy is more common, it's great.

Gotta get back to this

Collapse
miketalbot profile image
Mike Talbot Author

You are right, it's pretty like response.locals in Express (app.locals are really rather global), I haven't used KoaJS. The difference being that you don't have to pass response through to every function you call as cls is defined as a require.

Collapse
renoirb profile image
Renoir Boulanger

Koa with ctx.state is unique per request per user. Look the source up, it's great. I prefer it to Express. It's actually from some folks from Express. But the API is more modern. I imagine that Express 5 (in beta?) is inspired by it

Thread Thread
miketalbot profile image
Mike Talbot Author

Looks from the docs that you are still supposed to pass it on though? It's not a global like cls unless I'm missing something. If that's the case then you put it in cls and never pass it through function layers, callback and events etc.