小编Jac*_*Guy的帖子

通过请求获取Node.js中的下载进度

我正在创建一个使用Node模块下载应用程序文件的更新程序request.如何使用chunk.length估算剩余文件大小?这是我的代码的一部分:

var file_url = 'http://foo.com/bar.zip';
var out = fs.createWriteStream('baz.zip');

var req = request({
    method: 'GET',
    uri: file_url
});

req.pipe(out);

req.on('data', function (chunk) {
    console.log(chunk.length);
});

req.on('end', function() {
    //Do something
});
Run Code Online (Sandbox Code Playgroud)

progress download request node.js

26
推荐指数
4
解决办法
3万
查看次数

允许本地项目依赖于本地lerna包

我有一个正在开发的项目的lerna回购.它有几个相互依赖的包.为了简化开发,没有发布任何软件包,它们依赖于彼此的最新版本.

目录树

foo/
  packages/
    core/
      package.json
    errors/
      package.json
Run Code Online (Sandbox Code Playgroud)

富/包/核心/的package.json

{
  ...
  dependencies: {
    "@foo/errors": "*"
  }
}
Run Code Online (Sandbox Code Playgroud)

我有另一个项目,bar我正在用来测试lerna项目.目前我正在使用本地file:依赖关系链接到它的依赖项:

酒吧/的package.json

{
  ...
  dependencies: {
    "@foo/core": "../foo/packages/core"
  }
}
Run Code Online (Sandbox Code Playgroud)

这种方法给了我一个麻烦的世界.

  • 使用npm,我经常遇到ENOENT .DELETE错误.删除我的package-lock.json并重新安装已经花费了数年时间.
  • 使用纱,我已经无法yarn installbar.Yarn遵循file:依赖关系@foo/core,看到它依赖于@foo/errors并且不了解lerna的符号链接.这导致它失败,告诉我它找不到@foo/errors.

这使得为​​这个项目编写实际代码是次要的依赖管理.

我怎样才能做到这一点(我感觉相当简单?)项目结构工作?此时打开到lerna/yarn/npm/pnpm/shell脚本/ MS DOS.

npm lerna yarnpkg

24
推荐指数
3
解决办法
4973
查看次数

为什么在Flexbox中没有对象适合工作?

我有几个列,我使用flex给出相等的宽度.每个都包含img标签,我希望这些图像能够展示object-fit: cover尺寸.

.container {
  display: flex;
  flex-direction: row;
  width: 100%;
}
.test {
  flex: 1;
  margin-right: 1rem;
  overflow: hidden;
  height: 400px;
}
img {
  object-fit: cover;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div class="test"><img src="http://placehold.it/1920x1080"></div>
  <div class="test"><img src="http://placehold.it/1920x1080"></div>
  <div class="test"><img src="http://placehold.it/1920x1080"></div>
  <div class="test"><img src="http://placehold.it/1920x1080"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

图像没有调整大小,如本演示中所示.这是为什么?

html css flexbox

19
推荐指数
2
解决办法
1万
查看次数

CORB阻止动态AdSense广告

我有一个服务器端呈现的Angular应用程序,我在其中使用ng2-adsense在某些路由上显示AdSense广告.我注意到大部分时间(并非所有时间),我的广告都没有显示,我在Chrome开发者控制台中收到警告说:

Cross-Origin Read Blocking (CORB) blocked cross-origin response https://www.google.com/ads/measurement/l?ebcid=REDACTED with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
(anonymous) @ ads:1
Cross-Origin Read Blocking (CORB) blocked cross-origin response https://googleads4.g.doubleclick.net/pcs/view?xai=REDACTED &sig=REDACTED &urlfix=1&adurl= with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
Cross-Origin Read Blocking (CORB) blocked cross-origin response https://pagead2.googlesyndication.com/pagead/gen_204?id=sodar&v=24&t=2&bgai=REDACTED&bg=REDACTED with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
Run Code Online (Sandbox Code Playgroud)

我假设这是因为我的AdSense广告单元是动态加载的(必须是,广告只显示在客户端渲染上),而不是页面加载.

CORB链接文档引用了一种模糊算法,用于确定哪些资源负载是安全的.如何防止CORB阻止我的广告?

google-chrome adsense cors angular cross-origin-read-blocking

11
推荐指数
1
解决办法
958
查看次数

在不使docker缓存无效的情况下碰撞package.json版本

我正在使用一个非常标准的Dockerfile来容器化Node.js应用程序:

# Simplified version
FROM node:alpine

# Copy package.json first for docker build's layer caching
COPY package.json package-lock.json foo/
RUN npm install

COPY src/ foo/
RUN npm run build
Run Code Online (Sandbox Code Playgroud)

将我COPY分为两部分是有利的,因为它允许Docker缓存(长)npm install步骤。

但是最近,我开始package.json使用semver修改我的版本。这样做的副作用是使Docker缓存无效,从而npm install大大延长了我的构建时间。

我是否可以使用其他替代缓存策略,以便npm install仅在依赖项更改时运行?

node.js docker

7
推荐指数
2
解决办法
644
查看次数

Node.js表示静态服务器挂起图像 - 仅限Chrome

这是一个非常奇怪的错误,一直困扰着我多年.我有一个使用Express Static中间件的基本网站,以及渲染Jade的单独路由.

这是我的配置

app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

app.use(stylus.middleware({
    src      : __dirname + '/public',
    dest        : __dirname + '/public',
    compile : function(str, path)
    {
        return stylus(str)
        .set('filename', path)
        .set('compress', true) 
        .use(nib())
    }
}));


app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(cookieParser())
app.use(session({ secret: conf.sessionSecret, 
                        store: new RedisStore(),
                        saveUninitialized: true,
                        resave: true
}));

app.use(express.static(__dirname + '/public'))
Run Code Online (Sandbox Code Playgroud)

这是我的问题.在Chrome中的单个标签中精确刷新5次后,图片文件就会拒绝加载.所有其他内容都很好,只是图片和Chrome浏览器.为了解决这个问题,您必须打开一个新选项卡.我已经在Firefox中对此进行了测试 - 没有问题.这个错误不仅发生在这个应用程序中 - 它也发生在我的许多其他应用程序中.

由于这个项目目前正在开发中,我宁愿不以在线为例.请放心,这完全可以重现.有任何想法吗?

更新:我更新了Express 4,因为更新可能会解决我的问题.它没.您可以在此处看到我对此问题进行的屏幕演示.

更新:为了测试问题是否与静态服务器有关,我将代码修改为:

app.get('/img/*', function (req,res)
{
    console.log(req.url);
    console.log(path.extname(req.url));
    if (imgExt.indexOf(path.extname(req.url)) != -1)
    {
        res.sendfile('./public/'+req.url);
    }
}); …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome node.js express

6
推荐指数
1
解决办法
1355
查看次数

jQuery on()和stopPropagation()

我有一个从左侧滑入的侧窗格,使用get()动态添加内容.我希望窗格外的所有点击都能通过触发函数来关闭它.根据我读到的其他问题,我想出了以下代码.

$('html').click(function() {
    if (side_showing) {
        close_side();
    }
});

$(document).on("click", "#side", function(event) {  
    event.stopPropagation();
});
Run Code Online (Sandbox Code Playgroud)

我似乎无法使其发挥作用.我知道该on()功能正在被触发,但event.stopPropagation似乎并非如此.有小费吗?

jquery stoppropagation

5
推荐指数
1
解决办法
3147
查看次数

扩展原型函数 JavaScript

我有一个从另一个对象继承的对象,如下所示:

var a = function ()
{

}
a.prototype.foo = function ()
{
    bar();
}

var b = function ()
{
    a.call(this)
}
b.prototype = Object.create(a.prototype);
b.prototype.constructor = b;
Run Code Online (Sandbox Code Playgroud)

我想要一个 b 的方法,它也被命名为“foo”并使用相同的名称扩展 a 的函数。

b.prototype.foo = function ()
{
    baz();
    // When .foo() is called, runs both bar() and baz()
}
Run Code Online (Sandbox Code Playgroud)

是否有一种简单的方法可以在不借助库的情况下在原生 JavaScript 中完成此操作?

javascript inheritance prototype

5
推荐指数
1
解决办法
2120
查看次数

Angular 2子组件事件广播到父级

我想在Angular 2中的父指令中实现具有子指令的常见Angular 1.x模式.这是我想要的结构.

<foo>
  <bar>A</bar>
  <bar>B</bar>
  <bar>C</bar>
</foo>
Run Code Online (Sandbox Code Playgroud)

我希望这些Bar组件具有发送到click组件的单击事件.

这是我Foo到目前为止:

@Component({
  selector: 'foo',
  template: `
    <div>
      <ng-content></ng-content>
    </div>
  `
})
export class Foo {
   @ContentChildren(Bar) items: QueryList<Bar>;
}
Run Code Online (Sandbox Code Playgroud)

这是我的Foo:

@Component({
  selector: 'Bar',
  template: `
    <div (click)="clickity()">
      <ng-content></ng-content>
    </div>
  `
})
export class Bar {
  clickity() {
    console.log('Broadcast this to the parent please!');
  }
}
Run Code Online (Sandbox Code Playgroud)

Bar如果Foo点击其中一个,我该如何通知?

typescript angular2-directives angular

5
推荐指数
3
解决办法
2万
查看次数

Angular 2 RxJS检查延迟后鼠标事件是否仍然有效

我正在使用Angular 2来制定指令.我有以下事件绑定到主机组件:

host: {
    '(mouseenter)': 'onMouseEnter($event)',
    '(mouseleave)': 'onMouseLeave($event)'
}
Run Code Online (Sandbox Code Playgroud)

我还在指令上创建了两个流和监听器来管理这两个事件

export class PopupDirective {
    private _mouseEnterStream: EventEmitter<any> = new EventEmitter();
    private _mouseLeaveStream: EventEmitter<any> = new EventEmitter();

    onMouseEnter($event) {
         this._mouseEnterStream.emit($event);
    }

    onMouseLeave($event) {
         this._mouseLeaveStream.emit($event);
    }
}
Run Code Online (Sandbox Code Playgroud)

subscribe如果mouseenter事件在固定延迟后仍然有效(即,mouseleave没有发生),我希望仅调用我.我尝试这样做,但它不起作用(这是有道理的,我只是不知道如何解决它).

this._mouseEnterStream.flatMap((e) => {
  return Observable
    .of(e)
    .takeUntil(this._mouseLeaveStream);
}).delay(2000).subscribe(
  () => console.log('yay, it worked!')
);
Run Code Online (Sandbox Code Playgroud)

有Angular 2/RxJS经验的人是否知道如何处理这个问题?

rxjs rxjs5 angular

5
推荐指数
1
解决办法
2577
查看次数