V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ddddad
V2EX  ›  Node.js

怎么通过请求上下文异步传递信息

  •  
  •   ddddad · 109 天前 · 1166 次点击
    这是一个创建于 109 天前的主题,其中的信息可能已经有所发展或是发生改变。

    目前使用了 express-http-context2 , 但是感觉有 bug ,在后续中无法获取设置的值

    const app = express();
    app.use(middleware); // use http context
    
    if (!excludeAuth(req)) {
    	await verifyJWT(req, resp);
    console.log(get(HTTP_CONTEXT.ORG_ID));
    }
    
    
    3 条回复    2024-01-22 13:13:16 +08:00
    leopod1995
        1
    leopod1995  
       109 天前
    https://www.npmjs.com/package/express-http-context

    感觉可能是写法问题? 检查版本
    1. Install: npm install --save express-http-context
    (Note: For node v4-7, use the legacy version: npm install --save express-http-context@<1.0.0)

    2. Make sure you require express-http-context in the first row of your app. Some popular packages use async which breaks CLS.

    3. Node 10.0.x - 10.3.x are not supported. V8 version 6.6 introduced a bug that breaks async_hooks during async/await. Node 10.4.x uses V8 v6.7 in which the bug is fixed. See: https://github.com/nodejs/node/issues/20274.
    ddddad
        2
    ddddad  
    OP
       108 天前
    后面尝试了下,设置值不在 await 方法里可以获取,可能是跟这个上下是基于回调有关系,具体原因还没搞明白

    ps:感谢楼上
    stabc
        3
    stabc  
       97 天前
    没必要用这个插件啊,直接用 asynclocalstorage 一样简单,还可以自定义 type
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5334 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 08:18 · PVG 16:18 · LAX 01:18 · JFK 04:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.