通过节点中的Module.exports继承 [英] Inheriting through Module.exports in node

查看:375
本文介绍了通过节点中的Module.exports继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是我的愚蠢...

This is probably me being stupid...

我正在使用带有express的节点,我有一个单独的文件使用导出路由。在它上面,我需要并转换为变量,我使用npm安装的包。

I'm using node with express and I have a seperate file using exports for routes. Above it, I require and cast to a variable, a package I have installed using npm.

var passwordHash = require('password-hash');
app.get("/signup", routes.signup);

在routes.signup中,我有:

inside routes.signup, I have:

passwordHash.generate(req.form.username, {algorithm: 'sha512'})

并且它抛出一个错误,提示密码哈希是未定义的。我怎样才能继承所说的require调用?

and it throwing an error saying passwordHash is undefined. How can I go about "inheriting" said require call?

推荐答案

你也可以做以下事情(比如说这个代码定义于app.js):

You can also do the following (say this code is defined in app.js):

module.passwordHash = require('password-hash');
app.get("/signup", routes.signup);

var passwordHash = module.parent.passwordHash; // from app.js
passwordHash.generate(req.form.username, {algorithm: 'sha512'});

这篇关于通过节点中的Module.exports继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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