Tag: ecmascript 6

如何pipe理对共同请求模块和NodeJs中的文件的响应?

我使用Co-Request从http url中读取Zip文件,并且我有下面的代码从服务器读取.. 该代码已经工作。 但是我不知道如何写一个文件的响应Zip。 var co = require( "co" ); var request = require( "co-request" ); var options = { url: "http://www.example.com/sample.zip", headers: { 'Token': Appconfig.Affiliate_Token, 'Affiliate-Id' : Appconfig.Affiliate_Id } } console.log( "Downloading : zip file" ); var j = yield request( options ); 共同请求实际上是对Request的包装,我发现下面的代码将文件传输到stream中。 但不知道如何使用共同请求和yield来编写相同的代码。 request.get('http://example.com/img.png').pipe(request.put('http://example.com/img.png')) 请帮助如何使用良率和共同请求将响应zip写入文件

当前的JavaScript版本不支持string模板

我正在尝试在Node.js(v 5.7.0) 应用程序中使用ES6string模板 ,但它们不起作用。 Webstorm正确地警告了我 当前的Javascript版本不支持string模板 我相信我以前在节点应用程序中使用过string模板。 我怎样才能让string模板工作? 我用这个命令运行服务器 npm开始 当这是固定的,我怎么能帮助Webstorm知道它是固定的,并停止显示警告? 编辑:我改变了应用程序源以这种方式使用模板string: ““` var app = express(); var server = app.listen(process.env.PORT || 8080, function () { var host = server.address().address; var port = server.address().port; console.log(`App listening at http://${host}:${port}`); ““` 该应用程序打印: 应用程序在http:// ::: 8080处监听

ES6`export class A`等于`module.exports = A`吗?

当我看到巴贝尔编译的代码时,它们似乎并不相同。 实际上,前者转换为exports.A = A ,不等于module.exports = A (也许是module.exports.A = A ?) 那么有没有ES6风格的module.export = ? 或者语法仍然在ES6中? 或者ES6中不再推荐这种语法?

在WebStorm中debugging编译ES6 nodejs应用程序

我想debugging一个从babel编译的ES6文件运行的节点应用程序。 我有一个src文件夹中的ES6源代码和buildel文件夹中的babel ES5等价物。 Project root | | build | > ES5 Compiled files | | src | > ES6 Source files 我的目标:直接在我的ES6源代码中放置断点。 我已经生成了适当的源映射,并且我创build了一个节点debuggingconfiguration,该configuration运行ES5主文件,并将项目根设置为工作目录。 当我在ES5编译文件中设置断点时,可以中断,并自动显示我的ES6源代码。 不过,我想直接在ES6源代码中放置断点 。 这甚至可能吗? – > 2015-10-11编辑< 当我使用绝对的sourceRoot时,源映射现在可以在@ mockaroodev的configuration下 sourceRoot ! 然而 ,debugging仍然是打破:有时候会把我带到意想不到的地方。 似乎当该行暗含非内部(非本地) require时,debugging器将在所需内容结束时中断。 这是非常烦人的! 我在Linux上使用Webstorm 10.0.4,并将babel和sourcemaps升级到最新版本。 有人也遇到这个问题吗?

es6从同一个类中调用类方法

我想在我的类中调用一个类方法,如下面的例子所示。 import blah from './blaha'; export default class myclass{ constructor(con) { this.config = con; } async meth1(paramA) { //do_stuff… } meth2(paramB) { //attempt to call meth1() } } 我想从使用es6类样式的不同方法中调用一个方法。

什么是ES6生成器,如何在node.js中使用它们?

今天我在一个node.js聚会上,有一个我在那里遇到的人说node.js有es6生成器。 他表示,这是一个比callback风格编程的巨大改进,并且会改变节点格局。 Iirc,他说了一些关于调用栈和exception的东西。 我查看了他们,但是还没有真正发现任何能够以一种对初学者友好的方式解释他们的资源。 什么是生成器的高级概述,和callback有什么不同(或更好?)? PS:如果你可以给出一段代码来突出显示常见场景(做一个http请求或数据库调用)的区别,那真的很有帮助。

为什么instanceof在这里评估为真?

在这段代码中, f instanceof PipeWritable返回true(Node v8.4.0): const stream = require('stream'); const fs = require('fs'); class PipeWritable extends stream.Writable { constructor () { super(); } } const s = new PipeWritable(); const f = fs.createWriteStream('/tmp/test'); console.log(f instanceof PipeWritable); // true … ??? 对象s : Object.getPrototypeOf(s)是PipeWritable {} s.constructor是[Function: PipeWritable] PipeWritable.prototype是PipeWritable {} 对象f : Object.getPrototypeOf(f)是WriteStream { … } f.constructor是[Function: […]

为什么在nodejs的for循环中放慢速度?

我写了一个非常简单的基准: console.time('var'); for (var i = 0; i < 100000000; i++) {} console.timeEnd('var') console.time('let'); for (let i = 0; i < 100000000; i++) {} console.timeEnd('let') 如果您正在运行Chrome,则可以在此尝试(因为NodeJS和Chrome使用相同的JavaScript引擎,尽pipe通常略有不同): // Since Node runs code in a function wrapper with a different // `this` than global code, do that: (function() { console.time('var'); for (var i = 0; i < […]

有需求的Node.js ES6类

到目前为止,我已经通过以下方式在node.js创build了类和模块: var fs = require('fs'); var animalModule = (function () { /** * Constructor initialize object * @constructor */ var Animal = function (name) { this.name = name; }; Animal.prototype.print = function () { console.log('Name is :'+ this.name); }; return { Animal: Animal } }()); module.exports = animalModule; 现在用ES6,你可以像这样做“实际”类: class Animal{ constructor(name){ this.name = name […]

在nodejs项目中使用es6类的正确方法

我希望能够使用nodejs 4.1.2的酷es6类function 我创build了以下项目: a.js: class a { constructor(test) { a.test=test; } } index.js: require('./a.js'); var b = new a(5); 你可以看到我创build一个简单的类,它的构造函数获取一个参数。 并在我包括我需要这个类,并创build一个新的对象基于该类。 很简单..但仍然我得到以下错误: SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:413:25) at Object.Module._extensions..js (module.js:452:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) at require […]