Tag: 模块

导出对象中的对象

所以我一直在看这个post: Node.js module.exports的目的是什么?你如何使用它? 并一直试图在JavaScript对象的范围内理解它。 所以如果我在game.js有这个: var myObj = function() { this.function1 = function(){ … }; }; exports.myObj = myObj; 在另一个文件中,我假设我会做这样的事情: var RequiredObj = require('game.js'); 要么 var RequiredObj = require('game.js').myObj; 我迷失方向的是如何使用这个对象及其function,以及如何创build这个对象的新实例。 会是这样的吗? RequiredObj.myObj.function1(); 和 RequiredObj.myObj = new RequiredObj.myObj(); 对于我传递的对象有多大也有没有限制? 因为在我的实际脚本中,我的游戏对象是巨大的。

module.export不工作CompoundJS?

我是CompoundJS的新手。 我正在做一个示例应用程序。 如果按照以下教程: 使用mongoose模型 我已经在db / schema.js文件中写下了下面的代码: customSchema(function () { var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/my_database'); var Schema = mongoose.Schema, ObjectId = Schema.ObjectId; module.exports["mongoose"] = mongoose; module.exports["model"] = {} }); 在我的model.js文件(person.js)中,我有下面的代码: var Person = mongoose.model('Person', new module.schema()); model['Person'] = Person; 当我运行复合的时候 ,我得到了ReferenceError:没有定义mongoose。 任何帮助将是可观的。 提前致谢。

node.js:找不到模块“it-is”

我试图运行这个 node.jsunit testing,我得到这个错误: module.js:340 throw err; ^ Error: Cannot find module 'it-is' 从代码中可以看出,这个“它是”模块是一个unit testing模块 var it = require('it-is'); … it("Subject").equal(util.normalizeSubject("Re[2]:Subject")); 然而,谷歌这样的框架几乎是不可能的,因为谷歌只是删除了“它是”的一部分..我searchnode.jsunit testing无法find一个名为“它是”的东西.. 帮帮我!

我可以模拟像限制数据传输速度这样糟糕的互联网情况吗?

我使用node.js开发应用程序,当人们在互联网上冲浪的时候,我想模拟一些糟糕的互联网情况, 像限制数据传输速度,或延迟一些CSS或JS文件传输 我怎样才能实现这些目标?有没有一些目标可以做到这一点?

Nodejs引用module.exports

我试图做一些浏览器和nodejs服务器之间共享的js代码。 要做到这一点,我只是使用这些做法: http : //caolanmcmahon.com/posts/writing_for_node_and_the_browser/ 问题是当我想要导出一个函数,而不是一个对象。 在节点中,您可以执行如下操作: var Constructor = function(){/*code*/}; module.exports = Constructor; 这样当需求被使用时你可以这样做: var Constructor = require('module.js'); var oInstance = new Constructor(); 问题是当我尝试引用模块中的module.exports对象,并使用该引用来覆盖它与我的function。 在模块中它将是: var Constructor = function(){/*code*/}; var reference = module.exports; reference = Constructor; 为什么这不起作用? 我不想使用简单的解决scheme插入干净的代码,但我想明白为什么它是非法的,即使引用=== module.exports是真实的。 谢谢

我无法从node.js中的自定义模块接收数据

我写了一个名为accountManager.js的模块 var sqlite3 = require('sqlite3'); var db = new sqlite3.Database("./users.db"); exports.userExists = function userExists(nickName) { var stmt = 'SELECT * FROM users WHERE login="' + nickName + '"'; db.each(stmt,function(err,row) { if(row) { if(row.login==nickName) return true; else return false; } }); } 在我的主app.js文件中 var accountManager = require('./lib/accountManager'); console.log(accountManager.userExists('user1')); 这个应用程序在控制台中显示'undefined'…我检查模块工作正常,我想这是callback问题? 请给我一些帮助,我不明白这个代码有什么问题…

是否有可能在打字稿中使用nodejs风格的模块?

在节点中,我可以通过设置exports对象的属性来定义一个这样的模块: module.js exports.fun = function (val) { console.log(val); }; 并使用var module = require('module')来请求它并使用module.fun()函数。 是否可以像这样在TypeScript中定义模块: module.ts exports.fun = function (val :string) { console.log(val); }; 然后使用类似语法的节点(比如import module = require('module.ts')将其导入到其他某个文件中,以便将其编译为nodejs,但是如果现在在某些.ts文件中使用module.fun()它应该给我一个错误,如果参数不符合module.ts文件中指定的types。 我怎样才能在打字稿中做到这一点?

如何为兄弟目录中的所需脚本提供模块依赖关系

我正在运行一个node.js脚本( foo.js ) require位于同级目录中的助手脚本。 //foo.js: var magic = require('../util/magic'); magic.js使用一个npm模块express 。 但是,“程序”的主目录(包含package.json和node_modules )是node_modules所在的文件夹。 /program /node_modules /express .. /foo.js /package.json /util /magic.js 运行程序时,语句require('express')在magic.js中失败 – 找不到该模块。 有没有办法让node.js从program/node_modules目录加载express模块? 我想避免以下任何一项: 将node_modules目录移动到util和program的通用父项 添加一个node_modules目录到util 将所需模块的参考传递给magic.js 感谢你的帮助!

在模块中testingnode.js类

我正在写一个node.js模块,我的模块有一个私有类。 我试图写这个类的testing,但不知道如何做到这一点。 我的模块看起来像这样 var main = function(get_item){ var main_item = new MyClass(get_item); 返回main_item } 函数MyClass(item){ this.item = item; 返回this.init(); } MyClass.prototype = { init:function(){ 返回find_item(); }, find_item:function(){ //做了一堆东西来查找一个项目 }, update_item:function(){ / /一堆东西来更新项目 }, //这里有更多的方法 } module.exports = main //可能打开和closurestesting module.exports = new MyClass // ??? 不知道如何做到这一点 然后在我的spec文件夹里 var main = require(“./ modules / myClass”); 描述(“get […]

制作node.jsnetworking客户端

在为node.js内置networking模块创build客户端的所有示例中,我没有看到他们如何在客户端上运行networking,而无需下载它? 当然,如果我的客户端代码是这样开始的: var net = require('net'); var client = net.connect({port: 8124},function(){ console.log('client connected'); client.write('world!\r\n'); }); 那么首先我必须写: <script src="some strange node path to net ??"></script> 我对么? 我该怎么做呢? btw:node.js安装在我的服务器上的根目录不是本地的