Tag: ecmascript 6

需要外部模块作为ES6类实例variables

我有一个ES6类,这取决于一些外部模块的工作。 由于这是一个节点应用程序,我使用CommonJS来要求和加载模块。 然而,这种模块加载使得unit testing变得复杂,这并不是什么秘密。 我当然可以通过构造函数dependency injection所有必需的模块,但是这在dynamictypes语言中感觉很麻烦。 我也不喜欢使用像proxyquire这样的库,因为它膨胀了我的testing代码。 所以我想出了将所需模块存储为实例variables的想法。 例如: const someModule = require('some-module'); class MyClass { constructor() { this.someModule = someModule; } someFunction(value) { return this.someModule.someFunction(value); } } 这样我就可以使用模块加载器加载依赖关系,并在unit testing中仍然监视/存根/模拟它们。 这被认为是不好的做法,或者你可以看到任何重大的缺点?

节点承诺不推送所有的数据到数组

我正在使用AWS Node SDK来完成以下任务 – 获取地区 获取每个区域的集群ARN 获取经过筛选的ECS群集列表。 获取过滤群集的服务 我要脱钩的部分是第4步,列出每个群集的服务。 从本质上来说,每个集群列出服务的调用每个响应只能返回10个项目,因此会recursion调用该函数以查看是否存在另一个页面标记。 每个响应被推送到一个数组。 然后有一个最后的Promise.all打印出所有的答复。 问题是只有最初的调用被捕获,而不是任何下一个令牌的recursion调用。 任何帮助将非常感激 :) function getLiveCluster() { var liveClusterName = 'xx1-app-ecs' // Filter out the required clusters clustersAry.forEach(function(cluster) { if (cluster && cluster.clusterArns && cluster.clusterArns.length > 0) { cluster.clusterArns.forEach(function(clusterArns) { if (clusterArns.indexOf(liveClusterName) > -1) { var serviceParams = { cluster: clusterArns, maxResults: 10, nextToken: […]

如何从ES6中的parsing器中获得xml2js结果?

我正在Node中构build一个服务器,它将search一个文件夹以查看XML文件是否存在(glob),如果存在,请将(fs)中的文件作为JSON对象(xml2js)读取,并最终将其存储在数据库中某处。 我想要得到分析器的结果OUT到另一个variables,所以我可以用数据做其他事情。 据我所知,有些东西是同步运行的,但是我不知道如何阻止它,并且让我等待,直到它完成继续前进。 我将我的function从app.js分离到其他地方的控制器: app.controller.js const fs = require('fs-extra'); const glob = require('glob'); const xml2js = require('xml2js'); exports.requests = {}; exports.checkFileDrop = async () => { console.log('Checking for xml in filedrop…'); // this is the only place await works… await glob('./filedrop/ALLREQUESTS-*.xml', (err, files) => { var parser = new xml2js.Parser(); // this is looking for […]

运行navalia示例的打字稿错误

我试图从https://github.com/joelgriffith/navalia运行这个例子,但为了我的光,我不能没有错误地工作: navaliatest.ts /// <reference path="typings.d.ts" /> import { Chrome } from 'navalia'; const chrome = new Chrome(); async function buyItOnAmazon() { const url = await chrome.goto('https://amazon.com'); const typed = await chrome.type('input', 'Kindle'); const clicked = await chrome.click('.nav-search-submit input'); chrome.done(); console.log(url, typed, clicked); // 'https://www.amazon.com/', true, true } buyItOnAmazon(); tsconfig.json { "files": [ "navaliatest.ts" ], "compilerOptions": […]

Koa中的asynchronous函数奇怪的问题

这是代码片段: //Category.service.js … exports.update = async (ctx, next) => { const {categoryId} = ctx.params const _category = ctx.request.body // ctx.body = {key: 'test'} const category = await Category.get(categoryId) console.log(category) ctx.body = await category.update(_category) console.log(ctx.response) } … 当我发送请求时,它返回“未find”。 但terminal打印正确的结果: Category { id: 1, name: 'Javascript', description: 'This is a test for category update.' } { status: […]

为什么在重新分配错误之前在新块中使用letvariables?

我有以下文件https://www.codementor.io/cchilder/draft/bcgnrvw5p我试图解释这一点: // declare variables const x = 1; let y = 2; var z = 3; console.log(`Global scope – x, y, z: ${x}, ${y}, ${z}`); if (true) { console.log(`A new block scope – x, y, z: ${x}, ${y}, ${z}`); // redeclare variables // const x = 4; let y = 5; var z = 6; […]

如何将一个POST请求转发到两个外部URL?

我们使用的一个SaaS提供商有一个webook字段,但只允许input一个url。 事实上,我们需要将这个webhook发送到两个分析服务,所以我需要找出一个方法来编写一个自定义端点,将整个请求转发到我们需要的其他端点(目前为2个)。 用node和express做这个最简单的方法是什么? 如果我没有弄错,简单的redirect不适用于多个POST,对吧? 我不确定头文件甚至是请求内容是什么样子的,但是如果auth在头文件中,它需要尽可能地保留。 这是我迄今为止,但它是远远不够完整: app.post('/', (req, res) => { console.log('Request received: ', req.originalUrl) const forwardRequests = config.forwardTo.map(url => { return new Promise((resolve, reject) => { superagent .post(url) .send(req) .end((endpointError, endpointResponse) => { if (endpointError) { console.error(`Received error from forwardTo endpoint (${url}): `, endpointError) reject(endpointError) } else { resolve(endpointResponse) } }) }) }) Promise.all(forwardRequests) […]

不能用汇总的commonjs导入npm模块:“require is not defined”

我在ES6项目上工作,使用汇总和babel进行汇总。 除了当我尝试导入使用commonjs的npm模块(特别是require('something'))在我的浏览器中得到一个错误“require is not defined”(这意味着它没有正确编译节点模块从commonjs到ES5)。 但是,我使用rollup-plugin-node-resolve和rollup-plugin-commonjs ,如果我能正确理解,应该做这个工作。 这是我的汇总configuration文件: import babel from 'rollup-plugin-babel'; import eslint from 'rollup-plugin-eslint'; import resolve from 'rollup-plugin-node-resolve'; // to import node_modules packages easily import commonjs from 'rollup-plugin-commonjs'; // convert commonjs to es6 (in case you use require) export default { input: 'src/main.js', output: { file:'build/index.js', format: 'iife' }, sourcemap: 'inline', plugins: [ […]

Webpack如何直接导入webpack.config.babel.js?

我是一个很大的ReactJS项目的新手。 在顶层,它没有通常的webpack.config.js ,但只有一个`webpack.config.babel.js'。 这个确实得到使用,我可以validation,当运行 > webpack 被调用(通过npm run NODE_ENV =开发和WEBPACK_CONFIG =server_dev²,但这应该不重要)。 为什么? a)如果我删除了babel-config,我得到一个合理的投诉,即: configuration文件可以在当前目录中被命名为“webpack.config.js”。 b)相反,如果我添加自己的do-almost-nothing webpack.config.js ,那确实是“规则”(不再使用webpack.config.babel.js )。 所以,显然,如果这个文件丢失了,那么会有一个“不可用”的默认webpack.config.js 。 而且,显然呢,这个默认configuration以某种方式迎合了webpackconfiguration? 通过什么方式? 由于安装了这些节点模块? 还是.babelrc一个标记文件的sorting到webpack? 有趣的是,这个项目, preact-www项目也是一样的。 (也不是原始的webpackconfiguration,但webpack的味道)。 所以他们知道什么,我不…

在摩卡testing中可以使用ES6模块吗?

ES6,Windows 10 x64,Node.js 8.6.0,摩卡3.5.3 在摩卡testing中可以使用ES6模块吗? 我有export和import关键字的问题。 /* eventEmitter.js */ /* Event emitter. */ export default class EventEmitter{ constructor(){ const subscriptions = new Map(); Object.defineProperty(this, 'subscriptions', { enumerable: false, configurable: false, get: function(){ return subscriptions; } }); } /* Add the event listener. * @eventName – the event name. * @listener – the listener. */ addListener(eventName, […]