小编Sys*_*ter的帖子

异步/等待 Node.js https.get

我正在尝试使用 async / await 简化代码

但是在https.get使用 async / await 结构时有问题。

我知道如何使用第三方模块执行此操作,但更喜欢本机 node.jshttps模块。

下面的代码对我不起作用:

async function get_page() {

    const https = require('https')
    const url = 'https://example.com'

    const util = require('util')
    const https_get = util.promisify(https.get)

    const data = await https_get(url)

    do_awesome_things_with_data(data)
}
Run Code Online (Sandbox Code Playgroud)

此代码工作正常:

function get_page() {

    const https = require('https')
    const url = 'https://example.com'

    let data = ''

    https.get(url, res => {

        res.on('data', chunk => { data += chunk }) 

        res.on('end', () => {

           do_awesome_things_with_data(data)

        })
    }) 
}
Run Code Online (Sandbox Code Playgroud)

node.js async-await

9
推荐指数
2
解决办法
4484
查看次数

替换字符串中的前 N ​​个出现

如何替换N以下字符串中第一次出现的多个空格和制表符:

07/12/2017  11:01 AM             21523 filename with s p a c  e  s.js
Run Code Online (Sandbox Code Playgroud)

我期待以下结果:

07/12/2017|11:01|AM|21523|filename with s p a c  e  s.js
Run Code Online (Sandbox Code Playgroud)

我只通过N在同一个字符串上调用替换时间来知道不是很优雅的选项

.replace(/\s+/, "|").replace(/\s+/, "|").replace(/\s+/, "|");
Run Code Online (Sandbox Code Playgroud)

值得一提的是,我将在近 1,000,000 行上运行它,因此性能很重要。

javascript regex replace node.js

6
推荐指数
2
解决办法
2906
查看次数

在Node.js同步/异步性能和速度中移动100.000+个文件

我试图找出最快的方法而不影响性能,同时使用Node.js同步或异步移动100.000+文件

我有3个不同的循环由同步和异步测试forEach async.eachfor.循环迭代不会对时序结果产生太大影响.影响性能和速度的最重要的逻辑是同步或异步移动文件.

测试用例显示,同步renameSync()速度较慢,为20%,然后是异步rename().同时异步rename()使用3倍以上的CPU功率,然后同步renameSync().


我是否正确地认为使用同步更明智,renameSync()因为异步中的速度提升rename()不是那么重要且等于20%.虽然异步版本的CPU利用率开销似乎很大 - 300%?

或者我错过了什么?也许在速度/性能方面有更好的解决方法.

时间安排:

eachAsyncRenameAsync()

node rename.js  1.60s user 11.20s system 280% cpu 4.559 total
node rename.js  1.65s user 11.82s system 284% cpu 4.732 total
node rename.js  1.64s user 11.84s system 292% cpu 4.606 total


eachSyncRenameSync()

node rename.js  0.69s user 5.01s system 97% cpu 5.851 total
node rename.js  0.67s user 4.88s system 97% cpu 5.687 total
node rename.js  0.68s …
Run Code Online (Sandbox Code Playgroud)

javascript performance asynchronous node.js node-async

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

按类或名称获取嵌套子项的父元素

我有如下 html 元素,其层次结构未知

<div class="parent" name="parent">
  <div>
    <div>
      <div>
        <div onclick="get_parent(this)">
          Nested Child 1
        </div>
      </div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)
<div class="parent" name="parent">
  <div>
    <div onclick="get_parent(this)">
      Nested Child 2
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

有没有简单的方法可以按类或名称获取嵌套子项的父项?

也许有一些替代方案,child.parentElement或者child.parentNode我不知道哪个可以帮助我?

或者循环遍历所有父母直到我被需要class或者name是唯一可能的选择?

html javascript

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

如何在Node.js中设置DNS服务器解析查询?

我正在尝试8.8.8.8在Node.js解析查询中设置Google DNS服务器。

正确的方法是什么?在命令行中,通常我们可以执行以下操作:

$ nslookup stackoverflow.com 8.8.8.8

Server:     8.8.8.8
Address:    8.8.8.8#53

Non-authoritative answer:
Name:   stackoverflow.com
Address: 151.101.1.69
Address: 151.101.65.69
Address: 151.101.129.69
Address: 151.101.193.69
Run Code Online (Sandbox Code Playgroud)

但尚不清楚如何在Node.js中采用相同的方法

require('dns').resolve('stackoverflow.com', function (err, addresses) {
  console.log(err, addresses);
});

// => null [ '151.101.1.69', '151.101.65.69', '151.101.129.69', '151.101.193.69' ]
Run Code Online (Sandbox Code Playgroud)

dns node.js

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

如果目标文件不存在,则在 Node.js 中异步重命名文件

仅当目标文件不存在时,我才尝试在 Node.js 中异步重命名文件。

我做了一个快速测试,如下所示:

const fs = require('fs')

const files = [ 'file1', 'file2', 'file3' ]
const new_name = 'new-name' // same destination name for all 
Run Code Online (Sandbox Code Playgroud)

fs.exists() - 已弃用

for (let file of files)

  fs.exists(new_name, (exists) => {
    if (!exists) fs.rename(file, new_name, (err) => {})
   })
Run Code Online (Sandbox Code Playgroud)

fs.access() - 推荐

for (let file of files)

  fs.access(new_name, fs.constants.F_OK, (err) => {
    if (err) fs.rename(file, new_name, (err) => {})
  })
Run Code Online (Sandbox Code Playgroud)

fs.move() - 来自fs-extra

const fs_extra = require('fs-extra')

for (let file of …
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous fs node.js fs-extra

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

更改深层嵌套对象值

这似乎是显而易见的,但我无法弄清楚如何缩短代码中多个位置的深度嵌套对象值的更改,而无需每次重新键入该值的键路径:

> obj = { 'key1': { 'key2': { 'key3': { 'key4': { 'key5': 'value' }}}}}
> obj.key1.key2.key3.key4.key5
'value'
Run Code Online (Sandbox Code Playgroud)
> obj.key1.key2.key3.key4.key5 = 'changeme'
> obj.key1.key2.key3.key4.key5
'changeme'

> obj.key1.key2.key3.key4.key5 = 'changeme2'
> obj.key1.key2.key3.key4.key5
'changeme2'
Run Code Online (Sandbox Code Playgroud)

javascript object node.js

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