为node.jscallback实现超时

这是node.js中的典型情况: asyncFunction(arguments, callback); 当asynFunction完成时, callback被调用。 我用这种模式看到的一个问题是,如果asyncFunction 从不完成(并且asynFunction没有内置的超时系统),那么callback将永远不会被调用。 更糟糕的是,似乎callback没有办法确定asynFunction将永远不会返回。 我想实现一个“超时”,如果在1秒内没有被asyncFunction调用callback asyncFunction ,那么callback自动被调用,假设asynFunction出错了。 这样做的标准方式是什么?

只有在Node.js中不存在的情况下才能创build文件

我们有一个我们想要写入文件的缓冲区。 如果文件已经存在,我们需要增加一个索引,然后重试。 有没有办法创build一个文件,如果它不存在,或者我应该只是统计文件,直到我得到一个错误,find一个不存在已经? 例如,我有文件a_1.jpg和a_2.jpg 。 我想我的方法尝试创builda_1.jpg和a_2.jpg ,并失败,最后成功创builda_3.jpg 。 理想的方法看起来像这样: fs.writeFile(path, data, { overwrite: false }, function (err) { if (err) throw err; console.log('It\'s saved!'); }); 或者像这样: fs.createWriteStream(path, { overwrite: false }); 在节点的fs库中是否存在这样的事情? 编辑 :我的问题是,如果有一个单独的function,检查是否存在。 就是这样的:有没有办法在单个文件系统调用中创build一个文件?

如何撤消Object.defineProperty调用?

小提琴 var Assertion = function() { return { "dummy": "data" }; } Object.defineProperty(Object.prototype, 'should', { set: function(){}, get: function(){ return new Assertion(this); } }); // Insert magic here. // This needs to be false console.log(({}).should === undefined); 我有什么select在ES5中撤销defineProperty调用? 没有愚蠢的build议像Object.defineProperty = function() { }请。 下面的Object.defineProperty(Object.prototype, 'should', {}) 不起作用 和Object.defineProperty(Object.prototype, 'should', { value: undefined }) 抛出一个Uncaught TypeError: […]

我如何在节点中使用Amazon的Dynamodb Local?

Amazon 为他们的Dynamodb产品提供了一个本地模拟器,但这些例子只在PHP中 。 这些示例提到传递参数“base_url”来指定您正在使用本地Dynamodb,但在Node中返回此错误: { [UnrecognizedClientException: The security token included in the request is invalid.] message: 'The security token included in the request is invalid.', code: 'UnrecognizedClientException', name: 'UnrecognizedClientException', statusCode: 400, retryable: false } Dynamodb_local如何在Node中工作?

在Express NodeJS的其他路由中调用已经定义的路由

我正在使用Express在node.js中编写Web应用程序。 我已经定义了一条路线如下: app.get("/firstService/:query", function(req,res){ //trivial example var html = "<html><body></body></html>"; res.end(html) }); 我如何从expression式中重用这条路线? app.get("/secondService/:query", function(req,res){ var data = app.call("/firstService/"+query); //do something with the data res.end(data); }); 我找不到API文档中的任何内容,而不愿意使用另一个类似“请求”的库,因为这似乎是kludgey。 我试图让我的应用尽可能模块化。 思考? 谢谢

如何使用maven部署node.js应用程序?

我们的大部分团队都由java开发人员组成,因此整个构build/部署/依赖pipe理系统build立在maven之上。 我们使用CI,所以每个构build过程都运行unit testing(前端的业力和幻影,后端的茉莉节点)。 我设法为此configuration一个业力maven插件。 这并不能解决在build上从package.json下载node.js依赖关系的问题。 我需要在现有环境中部署我的node.js / express应用程序,所以完美的场景是: 从回购拉(使用maven构build自动完成) npm install (即 – 从节点包registry中下载依赖项) 运行testing 我试图为mavenfind一个nodejs包,但说实话 – 作为一个node.js开发人员,我不觉得在select正确的工具方面非常自信,因为我无法区分一个不好的maven插件一个体面的。 也许使用shell插件并从terminal调用npm install是一个更好的select? 你怎么看?

ipython相当于javascript / coffeescript为node.js?

更具体地说,是否有REPL有更多的丰富多彩的输出,漂亮的打印,选项卡完成和ipython对node.js javascript / coffeescript的其他好处?

testing在节点中使用mocha / supertestredirect的请求

我似乎无法得到以下的集成testing,通过使用摩卡 , supertest , 应该 (和coffeescript)的快速项目。 考试 should = require('should') request = require('supertest') app = require('../../app') describe 'authentication', -> describe 'POST /sessions', -> describe 'success', (done) -> it 'displays a flash', (done) -> request(app) .post('/sessions') .type('form') .field('user', 'username') .field('password', 'password') .end (err, res) -> res.text.should.include('logged in') done() 相关的应用程序代码 app.post '/sessions', (req, res) -> req.flash 'info', […]

是否有可能使用node.js开发Google Chrome扩展?

我想使用node.js开始开发Google Chrome浏览器扩展程序(因为我已经在node.js中编写了“文本到歌曲”脚本,我想将它转换为Chrome扩展。)将是解决这个问题最直接的方法?

Grunt需要在系统PATH警告中安装指南针

我想安装Yeoman,Grunt和凉亭到我的节点项目。 我在Eclipse中创build了一个节点项目(使用nodeclipse插件),并在cmd中导航到项目文件夹(H:\ Eclipse Workspace \ YoTest),并input: npm install yo -g npm install generator-webapp -g yo webapp select:Sass(带指南针),以cmd为单位 然后键入: grunt serve 运行webapp。 我收到以下错误信息: Done, without errors. Warning: Running "compass:server" (compass) task Warnin: You need to have Ruby and Compass installed and in your system PATH for this task to work. More info: https://github.com/gruntjs/grunt-contrib-compass Use –force to […]