Tag: dependency injection

节点js的 – 导入mssql模块在Angular 2项目中使用的错误:“不能解决'dns'”和“无法parsing'dgram'”

我在Angular 2和NodeJS开发方面都有基本的经验。 如果这个问题太简单了,我很抱歉。 我的目标是创build一个连接到SQL Server数据库的Web界面。 这个应用程序将使用户能够轻松地发送查询和从应用程序的连接数据库接收结果。 search解决scheme,我决定在通过npm安装后,将mssql模块导入到我的mssql-connection.service.ts 。 我打算通过使用dependency injection或其常规语法在服务中使用它。 但是,在编译这些情况时,我遇到了错误。 首先,试图通过import { MSSQL } from 'mssql';写入import { MSSQL } from 'mssql';来使用DI方法import { MSSQL } from 'mssql'; 我得到两个重复的错误消息node_modules/@types/mssql/index"' has no exported member 'MSSQL'. 其次,为了弥补这种情况,我编写了import 'mssql'; 。 然后,我总共得到三个错误消息:我得到两个重复的错误消息Can't resolve 'dns' in '…\node_modules\tedious\lib'和一个错误消息Can't resolve 'dgram' in '…\node_modules\tedious\lib'我也试过使用const sql = require('mssql'); 但是这会给我Cannot find name 'require'. 错误消息,前面是以前的错误消息。 我很难过,想知道为什么会发生这种情况! 我希望能够自由地使用外部模块。 […]

Node.js,Express和dependency injection

我在一个node.js项目的早期阶段,我正在寻求改善整个应用程序的组织。 在过去,我使用了Symfony2(PHP),现在我在Angular中编写了很多代码,这两个代码都非常依赖DI。 所以,我非常喜欢在我的node.js项目中应用相同的原则。 我知道像Rewire这样的软件包的存在,但现在我想尝试DI方法。 问题是, 如何达到一个平衡,以保持轻量级的感觉,使节点与一个经过良好testing的dependency injection的应用程序的坚实 (我知道,经过充分testing是什么给了坚实;-))。 节点模块 其中一个问题就是如何pipe理外部模块,如果某个对象需要fs模块怎么办? 正如Vojta Jina(来自AngularJS)在这篇文章中所述 : 所以现在最适合我的方式就是这样的:模块是无状态的。 它们只包含类/函数/常量的定义。 所以,我想我必须注入一切: function Foo(fs) { this.fs = fs; } Foo.prototype.doSomething: function () { // this.fs… }; module.exports = Foo; 某处: var fs = require('fs'); var Foo = require('./Foo'); var foo = new Foo(fs); foo.doSomething(); performance 由于Express使用apply()来调用处理程序,所以上下文丢失,我们不能使用this 。 所以我们留下这些: // foo.js function […]

如何创build一个可重用的Socket.IO模块

我有创build一个暴露我的Socket.IO库function的模块的麻烦: const sio = require('socket.io'); module.exports = function(server) { const io = sio(server); return { register: function(namespace) { let nsp = io.of(namespace); nsp.on('connect', function(socket) { // … } } } } 现在的问题是如何在其他模块中使用这个? 在我的app.js 我使用Express创buildserver ,并可以使用require('./mysocketio')(server)实例化模块,但不能在其他模块中使用,因为服务器在那里不可用。 什么是解决这些循环依赖的好方法?

NodeJS和Javascript(requirejs)dependency injection

我目前使用requirejs来pipe理模块js / css的依赖。 我想通过一个集中的configuration文件来发现有节点做这个的可能性。 所以,而不是手动做类似的东西 define([ 'jquery' 'lib/somelib' 'views/someview'] 在每个模块内。 我会有节点注入依赖关系即 require('moduleA').setDeps('jquery','lib/somelib','views/someview') 无论如何,我感兴趣的任何项目在dependency injection节点。 谢谢

dependency injection库 – 重命名注入的值

我想按名称注入lodash,如下所示: let val = function(lodash){ // lodash will be injected, simply by using require('lodash'); }; 但是说我想重命名导入,我想要做这样的事情: let val = function({lodash:_}){ }; 要么 let val = function(lodash as _){ }; 有没有办法做到这一点与ES6 / ES7 / ES8或TypeScript? 请注意,这个DI框架比只需要('x')做更多的工作…它将尝试注入其他值首先,如果没有其他的存在,那么它将尝试要求的价值。 还要注意,这里的要求是,当你调用val.toString()时,“lodash”将被视为参数名称。 但是在函数体内的运行时会看到_而不是lodash。 这是因为为了注入lodash,我们调用fn.toString()来获取参数名称。

存根Date.now()和Math.random()

我正在用Sinon使用Mocha来testing我的node.js模块。 我已经成功地嘲笑了其他的依赖(我写的其他模块),但是我遇到了困扰非纯函数(如Math.random()和Date.now() )的问题。 我已经尝试了以下(简化,以便这个问题不是如此本地化),但Math.random()不存在,因为一个明显的范围问题。 Math的实例在testing文件和mymodule.js之间是独立的。 test.js var sinon = require('sinon'), mymodule = require('./mymodule.js'), other = require('./other.js'); describe('MyModule', function() { describe('funcThatDependsOnRandom', function() { it('should call other.otherFunc with a random num when no num provided', function() { sinon.mock(other).expects('otherFunc').withArgs(0.5).once(); sinon.stub(Math, 'random').returns(0.5); funcThatDependsOnRandom(); // called with no args, so should call // other.otherFunc with random num other.verify(); // ensure […]

分叉一个subprocess并注入依赖

我目前有一个模块是阻塞的操作,所以我正在考虑把它变成一个subprocess,而不是叉。 如果我想这样做,那么我当然需要修改我的模块的架构。 该模块要求dependency injection通过调用模块作为一个函数,传入依赖关系,如下所示: var dependency = { name: "Bob" } require('worker')(dependency) 然后在我的worker模块中: module.exports = function (dependency) { // Outputs { name: "Bob" } console.log(dependency) } 我怎样才能把这个例子变成一个分叉的subprocess呢?