如何在 node.js 脚本中执行命令行参数
How can I execute command line arguments in a node.js script
我是编程的新手,我正在努力 - 也许这个问题真的不值得问,但我没有取得任何进展,因为...太久了。
我正在尝试在 node.js 脚本中对连接到本地网络的所有设备进行发现搜索。
我发现这个 npm 模块应该可以完成这项工作:https://www.npmjs.com/package/local-devices/v/3.0.0
所以我只是尝试复制粘贴他们给出的示例来搜索我的网络以查找所有设备:
const find = require('local-devices');
// Find all local network devices.
find().then(devices => {
devices /*
[
{ name: '?', ip: '192.168.0.10', mac: '...' },
{ name: '...', ip: '192.168.0.17', mac: '...' },
{ name: '...', ip: '192.168.0.21', mac: '...' },
{ name: '...', ip: '192.168.0.22', mac: '...' }
]
*/
})
但是如果我现在运行脚本,什么也不会发生。
到目前为止我尝试了什么:
1. 我试图将输出保存在一个常量中,并试图在控制台上输出它
const found = find().then(devices => {
devices
/*[
{ name: '?', ip: '192.168.0.10', mac: '...' },
{ name: '...', ip: '192.168.0.17', mac: '...' },
{ name: '...', ip: '192.168.0.21', mac: '...' },
{ name: '...', ip: '192.168.0.22', mac: '...' }
]
return devices
})
console.log(found);
那么控制台输出是Promise { <pending> }
。我还不知道该怎么办。
- 有这个命令行参数
arp -a
,如果在命令行中执行,它会在终端中列出网络中所有设备的所有 IP 地址。所以它基本上完全符合我的要求,但我需要在以下代码中处理该输出(我需要从此列表中找到智能插头的一个特定 IP 地址,但这是另一回事)。
如何获取此命令行参数,并在我的 javascript 代码/我的 node.js 脚本中执行它,并将输出保存在 variable/const 中?
此致,
迈克尔
当一个函数 return 是 Promise
时,这意味着它是 运行 异步的(网上有各种关于此的教程)。长话短说,find()
函数将立即使用 Promise
对象 return 然后它会去寻找 IP 地址,一旦找到它就会填充 Promise
(即fulfill
它)。
then()
部分是 Promise 完成后执行的回调。所以你可以做的是:
const find = require('local-devices');
async function findIP() {
// Find all local network devices.
const found = find().then(devices => {
console.log(devices);
})
await found;
}
findIP();
本质上,await
告诉 JS 等到 found
Promise 对象完成 - 现在 JS 只是在找到 ip 地址之前退出。
当然值得一问! JS 可能是一门令人困惑的语言。
如果函数 returns 是 Promise(如 find
所做的那样),那么记录它的内容 returns 通常不是很有用。 Promise 是在某个时候能够提供某种有用价值的东西。
函数的 .then
部分是您可以使用承诺包含的值的地方。 Promise 将 运行 包含在 .then
中的函数一旦取回值。所以做这样的事情:
const find = require('local-devices');
// Find all local network devices.
find().then(devices => {
console.log(devices);
})
表示当找到设备的函数返回一个值时,它将记录该值。
您面临的问题在 JS 世界中非常常见。我会尽力向您解释。它被称为并发,用非常简单的术语来说,它意味着两个或多个进程(或线程)运行在一起,但不是同时。一次只有一个进程执行。
更简单:这意味着 then
回调中的代码在 CPU 空闲时被延迟。
您在打印 found
对象时看到的是 Promise { <pending> }
。它是一个对象,表示异步操作的最终完成(或失败)及其结果值。它允许您将 handlers/callbacks 与异步操作的最终成功值或失败原因相关联。这让异步方法 return 值像同步方法一样:不是立即 return 最终值,异步方法 return 承诺在将来的某个时间点提供值。
但我们为什么要关心?你可能会说。嗯,这是因为您可以简单地将 find
函数视为 "linear" 操作。你必须想象你传递了某种回调(通过then
),一旦找到所有本地网络设备就会执行。
因此您可以像这样重写代码:
const find = require('local-devices');
// Find all local network devices.
find().then(devices => {
console.log(devices) /*
[
{ name: '?', ip: '192.168.0.10', mac: '...' },
{ name: '...', ip: '192.168.0.17', mac: '...' },
{ name: '...', ip: '192.168.0.21', mac: '...' },
{ name: '...', ip: '192.168.0.22', mac: '...' }
]
*/
})
But what if you wanted to avoid the callback hell
好吧,JavaScript 提供了 async
& await
关键字来帮助您。这只是 hiding/dealing 回调的一种奇特方式。
首先,我们需要在 async
函数中,这是使用 await
的要求。在下面的示例中,我将使用自调用函数来使事情变得更好。然后我们可以使用 await
从 find
函数中得到结果:
(async () => {
const find = require('local-devices');
// Find all local network devices.
const devices = await find()
console.log(devices)
})()
我是编程的新手,我正在努力 - 也许这个问题真的不值得问,但我没有取得任何进展,因为...太久了。
我正在尝试在 node.js 脚本中对连接到本地网络的所有设备进行发现搜索。
我发现这个 npm 模块应该可以完成这项工作:https://www.npmjs.com/package/local-devices/v/3.0.0
所以我只是尝试复制粘贴他们给出的示例来搜索我的网络以查找所有设备:
const find = require('local-devices');
// Find all local network devices.
find().then(devices => {
devices /*
[
{ name: '?', ip: '192.168.0.10', mac: '...' },
{ name: '...', ip: '192.168.0.17', mac: '...' },
{ name: '...', ip: '192.168.0.21', mac: '...' },
{ name: '...', ip: '192.168.0.22', mac: '...' }
]
*/
})
但是如果我现在运行脚本,什么也不会发生。
到目前为止我尝试了什么: 1. 我试图将输出保存在一个常量中,并试图在控制台上输出它
const found = find().then(devices => {
devices
/*[
{ name: '?', ip: '192.168.0.10', mac: '...' },
{ name: '...', ip: '192.168.0.17', mac: '...' },
{ name: '...', ip: '192.168.0.21', mac: '...' },
{ name: '...', ip: '192.168.0.22', mac: '...' }
]
return devices
})
console.log(found);
那么控制台输出是Promise { <pending> }
。我还不知道该怎么办。
- 有这个命令行参数
arp -a
,如果在命令行中执行,它会在终端中列出网络中所有设备的所有 IP 地址。所以它基本上完全符合我的要求,但我需要在以下代码中处理该输出(我需要从此列表中找到智能插头的一个特定 IP 地址,但这是另一回事)。
如何获取此命令行参数,并在我的 javascript 代码/我的 node.js 脚本中执行它,并将输出保存在 variable/const 中?
此致, 迈克尔
当一个函数 return 是 Promise
时,这意味着它是 运行 异步的(网上有各种关于此的教程)。长话短说,find()
函数将立即使用 Promise
对象 return 然后它会去寻找 IP 地址,一旦找到它就会填充 Promise
(即fulfill
它)。
then()
部分是 Promise 完成后执行的回调。所以你可以做的是:
const find = require('local-devices');
async function findIP() {
// Find all local network devices.
const found = find().then(devices => {
console.log(devices);
})
await found;
}
findIP();
本质上,await
告诉 JS 等到 found
Promise 对象完成 - 现在 JS 只是在找到 ip 地址之前退出。
当然值得一问! JS 可能是一门令人困惑的语言。
如果函数 returns 是 Promise(如 find
所做的那样),那么记录它的内容 returns 通常不是很有用。 Promise 是在某个时候能够提供某种有用价值的东西。
函数的 .then
部分是您可以使用承诺包含的值的地方。 Promise 将 运行 包含在 .then
中的函数一旦取回值。所以做这样的事情:
const find = require('local-devices');
// Find all local network devices.
find().then(devices => {
console.log(devices);
})
表示当找到设备的函数返回一个值时,它将记录该值。
您面临的问题在 JS 世界中非常常见。我会尽力向您解释。它被称为并发,用非常简单的术语来说,它意味着两个或多个进程(或线程)运行在一起,但不是同时。一次只有一个进程执行。
更简单:这意味着 then
回调中的代码在 CPU 空闲时被延迟。
您在打印 found
对象时看到的是 Promise { <pending> }
。它是一个对象,表示异步操作的最终完成(或失败)及其结果值。它允许您将 handlers/callbacks 与异步操作的最终成功值或失败原因相关联。这让异步方法 return 值像同步方法一样:不是立即 return 最终值,异步方法 return 承诺在将来的某个时间点提供值。
但我们为什么要关心?你可能会说。嗯,这是因为您可以简单地将 find
函数视为 "linear" 操作。你必须想象你传递了某种回调(通过then
),一旦找到所有本地网络设备就会执行。
因此您可以像这样重写代码:
const find = require('local-devices');
// Find all local network devices.
find().then(devices => {
console.log(devices) /*
[
{ name: '?', ip: '192.168.0.10', mac: '...' },
{ name: '...', ip: '192.168.0.17', mac: '...' },
{ name: '...', ip: '192.168.0.21', mac: '...' },
{ name: '...', ip: '192.168.0.22', mac: '...' }
]
*/
})
But what if you wanted to avoid the callback hell
好吧,JavaScript 提供了 async
& await
关键字来帮助您。这只是 hiding/dealing 回调的一种奇特方式。
首先,我们需要在 async
函数中,这是使用 await
的要求。在下面的示例中,我将使用自调用函数来使事情变得更好。然后我们可以使用 await
从 find
函数中得到结果:
(async () => {
const find = require('local-devices');
// Find all local network devices.
const devices = await find()
console.log(devices)
})()