如何在Node/Koa服务器中访问通过require添加的库 [英] How do I access libraries I'm adding via require, in my Node/Koa server

查看:97
本文介绍了如何在Node/Koa服务器中访问通过require添加的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的Node应用程序(Koa服务器)中需要一个外部库.我在主server.js文件var njwt = require('njwt');

I'm trying to require an external library in my Node app (Koa server). I'm adding njwt in my main server.js file var njwt = require('njwt');

但是我无法访问njwt,在我的路由处理程序函数中,它给出一个错误,提示njwt未定义.

But I can't access njwt, in my route handler function it gives an error saying njwt is undefined.

从此答案中( https://stackoverflow.com/a/5809968 ),似乎在我的计算机中使用了严格模式server.js主文件使我导入的文件中定义的函数和变量无法访问.

From this answer (https://stackoverflow.com/a/5809968), it seems that using strict mode in my main server.js file makes functions and variables defined in my imported file inaccessible.

但是解决方法是什么?

推荐答案

我认为问题在于如何将njwt实例发送到路由器, 您可以像这样传递njwt实例,

I think the issue is how to send njwt instance to your router, You can pass njwt instance like this,

require('./routes')(njwt);

require('./routes')(njwt);

这篇关于如何在Node/Koa服务器中访问通过require添加的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆