Tag: commonjs

将tweetNaCL-jsencryption库添加到CouchDB validate_doc函数中

我正在尝试使用公钥encryption( tweetnacl )来validation文档。 我知道你可以添加commonjs模块来validationfunction,但一直未能。 { "_id": "_design/validate_update", "language": "javascript", "validate_doc_update": "function(newDoc, oldDoc, userCtx){ verify=require('lib/validation').sign.detached.verify; if(verify(newDoc.message, new.Doc.signature, oldDoc.publicKey)){ return true; } }", "lib": { "validation": "exports.nacl=(function(nacl){….. })" } } 当我这样做时,我得到的错误: Module require('lib/validation') raised error (new TypeError("func.apply is not a function", "/usr/local/share/couchdb/server/main.js", 1181)) 我想我必须以某种方式改变tweetnacl代码被解释为commonjs模块?

编写commonjs模块并使用require来加载它(不使用相对path)

在不使用相对path的情况下引用本地commonjs模块的最佳实践是什么? var custMod= require('customModule'); custMod(true); custMod.save('foo'); 有没有build立这样的模块的任何参考? 如果我写下面的模块,当我调用custMode.save(12); module.exports = customModule;function customModule(mode) { var mode = 1; return { save: function (value) { console.log(mode, value) }, get: function (id) { console.log(mode, id) } }

CommonJS模块 – 导出一个返回带参数的函数的函数

在这个例子中,有一个快速的中间件模块。 module.exports = function incrementCounter() { return function incrementCounter(req, res, next) { var app = req.rendrApp , count = app.get('session').count || 0; req.updateSession('count', count + 1); next(); }; }; 你能不能用以下方法达到同样的效果? module.exports = function incrementCounter(req, res, next) { var app = req.rendrAp , count = app.get('session').count || 0; req.updateSession('count', count + 1); next(); }; 我的问题是,为什么你会导出一个函数返回一个函数的参数? 对于前者有没有什么好处,我不知道?

自动方式导出每个function作为主要的方法

我只是创build一个包含许多函数的单个文件的节点模块,例如: function alpha () { return 'alpha' } function beta () { return 'beta' } function main () { return alpha() + beta() + 'gamma' } module.exports = main 这个问题是,我真正想要的是所有声明的函数(其他main )是main方法,如main.alpha (注意在这种情况下main不会是一个对象与原型)。 我想所有这些函数作为方法,所以我仍然可以导出一个functionmain并能够使用这个模块之外的main.alpha() 。 有了这个说我坚持与模块中的function,我创作他们的方式。 我可以进入并更改每个函数声明( function (.+) \( ) main.$1 = function (用我的文本编辑器(primefaces的)查找和replace正则expression式函数。 我创build的模块中有20个function,编辑所有的代码非常繁琐和复杂。 另一个问题是,每个函数的名字变得不那么容易识别,而不是一个很好的getFiles函数,我有一个不必要的long main.getFiles函数。 我期望的结果是这个效果(图1): function main () { return main.alpha() + […]

为什么npm包经常使用相对path进行require(例如../../file)?

npm上的一些软件包似乎使用require函数的方式如下: var CounterActions = require('../actions/counter'); 而不是这个: var CounterActions = require('mypackage/actions/counter'); 这是为什么? 我看不到任何好处,而且很难将文件移到其他位置。 另外,读取代码比较困难,因为你不知道需求是指什么(例如require("../../../../../../index.js") )

Typescript:从另一个模块的类中添加一个方法/从不同模块填充一个名称空间

故事 我正在build立一个math运算模块库。 我也想把这个库分成多个模块: core , relational , vectors等等。 这些模块可以单独使用(但都取决于core模块) 我知道这是不可能使用部分类如何将TypeScript类分成多个文件? / https://github.com/Microsoft/TypeScript/issues/563 问题 : core模块定义了Set类,它是一个math集。 它定义了诸如Set#add , Set#remove 。 但是, 可选的 relational模块在Set类上添加了Set#product运算符。 其他模块也可以在Set类中添加其他操作。 我想保持添加function的可能性,当我看到适合。 这个问题 使用打字稿 ,我怎样才能添加一个方法在另一个模块居住的类? 我如何安排types,以便我的图书馆用户只有在安装了relational模块的情况下才能在代码完成中看到Set#product ? 否则,他只看到#add和#remove操作? 我正在开发node.js这个库,但也使用browserify捆绑它的浏览器使用。 // core/set.ts export class Set { add(element){} remove(element){} } // relational/set.ts import {Set} from './../core/set.ts'; Set.prototype.product = function(){} // ? // app/index.js import {core} […]

骨干localStorage不使用良好的同步方法

我疯了,我的问题! 我有一个模型,我想使用localStorage,但是当我在我的视图中调用model.fetch,我得到错误“错误:一个”url“属性或函数必须指定”。 这个错误是“正常的”,如果我不使用localStorage,因为我没有定义的url属性。 这里的fetch调用应该使用Backbone.sync(也就是localStorage模块的Backbone.localSync覆盖吧?)。 但它永远不会进入localStoragefunction! 就好像模块从未加载过一样。 但我添加了一些console.log无处不在和backbone.localstorage加载并正确初始化。 这就像骨干重新加载的地方,我松散覆盖Backbone.sync指向localStorage.localSync … 错误发生在modules / config.js,render函数中。 这是我的代码: models / configuration.js': var Backbone = require('Backbone'); Backbone.LocalStorage = require('Backbone.LocalStorage'); // Exports the module module.exports = Backbone.Model.extend({ defaults:{ id: 1 agencyName : 'xxx', agencyId : 'xxx', serviceUrl : 'xxx' }, localStorage: new Backbone.LocalStorage('Configuration') }); data / configuration.js: 'use strict'; // Get class […]

加载GoCardless SDK,需要不工作

当我尝试加载GoCardless SDK,并在configuration文件 nodejs中inputcongfig时,抛出以下错误: var gocardless = require('gocardless')(gcConfig); ^ TypeError: require(…) is not a function at Object.<anonymous> (/Users/joe/projects/adultcms-main-site/routes/views/register.js:10:39) at Module._compile (module.js:413:34) at Object.Module._extensions..js (module.js:422:10) at Module.load (module.js:357:32) at Function.Module._load (module.js:314:12) at Module.require (module.js:367:17) at require (internal/module.js:16:19) at /Users/joe/projects/adultcms-main-site/node_modules/keystone/lib/core/importer.js:42:23 at Array.forEach (native) at importer (/Users/joe/projects/adultcms-main-site/node_modules/keystone/lib/core/importer.js:32:26) at Object.<anonymous> (/Users/joe/projects/adultcms-main-site/routes/index.js:31:9) at Module._compile (module.js:413:34) at Object.Module._extensions..js (module.js:422:10) at Module.load (module.js:357:32) […]

避免重新评估和dynamic卸载用`require`调用的对象

我正在研究nodejs模块系统是如何工作的。 迄今为止我发现了这些文献: https://nodejs.org/api/modules.html http://fredkschott.com/post/2014/06/require-and-the-module-system/ http://www.bennadel.com/blog/2169-where-does-node-js-and-require-look-for-modules.htm 这有助于我理解几点,但仍然有这些问题: 如果我有一个昂贵的资源(假设数据库池连接)在一个模块内,我怎么能确保通过再次要求模块我不重新评估资源? 我如何dynamic卸载一次性资源,一旦我已经调用了“要求”呢? 这很重要,因为我有一些场景要求我确保拥有一个数据库池实例。 因为这个我出口模块,能够接收参数,而不是只需要昂贵的资源。 任何指导,非常感激。

加载“香草”Javascript库到Node.js ='意外的令牌导出'

基于相关问题构build:将 “Vanilla”Javascript库加载到Node.js中 我正在尝试使用上面问题中Chris W.所描述的方法将“Vanilla”Javascript库“rsa.js”加载到NodeJS中。 实质上,我创build了一个自定义的node_modules目录,如下所示: ./node_modules/rsa/rsa-lib/rsa.js ./node_modules/rsa/rsa-lib/README ./node_modules/rsa/index.js 唯一的问题是,在rsa.js的最后一行编译模块失败: undefined:836 export default rsa; SyntaxError: Unexpected token export rsa.js var rsa = {}; (function(rsa) { … })(rsa); console.log("rsa",rsa); export default rsa; index.js var fs = require('fs'); // Read and eval library filedata = fs.readFileSync('./node_modules/rsa/rsa-lib/rsa.js','utf8'); eval(filedata); /* The rsa.js file defines a class 'rsa' which is all […]