- 檔案系統模組
- PhantomJS - 屬性
- PhantomJS - 方法
- 系統模組
- PhantomJS - 屬性
- Web 伺服器模組
- PhantomJS - 屬性
- PhantomJS - 方法
- 雜項
- 命令列介面
- PhantomJS - 螢幕捕獲
- PhantomJS - 頁面自動化
- PhantomJS - 網路監控
- PhantomJS - 測試
- PhantomJS - REPL
- PhantomJS - 例項
- PhantomJS 有用的資源
- PhantomJS - 快速指南
- PhantomJS - 有用的資源
- PhantomJS - 討論
Webpage 子程序模組
Phantomjs 子程序模組有助於與子程序進行互動,並使用 **stdin /stdout/stderr** 與它們通訊。子程序可用於 **列印、傳送郵件** 或 **呼叫以其他語言編寫的程式** 等任務。要建立一個子程序模組,需要對其進行引用。
例如 −
var process = require("child_process");
Spawn 方法
使用 spawn 子程序,可以訂閱其 **stdout** 和 **stderr** 流,以即時獲得資料。
語法
其語法如下 −
var spawn = require('child_process').spawn;
示例
我們來看一個 spawn 方法的示例。
var process = require("child_process")
var spawn = process.spawn
var child = spawn("cmd", ['/c', 'dir']);
child.stdout.on("data", function (data) {
console.log("spawnSTDOUT---VALUE:", JSON.stringify(data))
})
child.stderr.on("data", function (data) {
console.log("spawnSTDERR:", JSON.stringify(data))
})
child.on("exit", function (code) {
console.log("spawnEXIT:", code)
})
輸出
上述程式會生成以下輸出。
spawnSTDOUT---VALUE: " Volume in drive C is OS\r\n" spawnSTDOUT---VALUE: " Volume Serial Number is 7682-9C1B\r\n\r\n Directory of C: \\phantomjs\\bin\r\n\r\n" spawnSTDOUT---VALUE: "20-05-2017 10:01 <DIR> .\r\n20-05-2017 10:01 <DIR> ..\r\n13-05-2017 20:48 12 a,txt.txt\r\n07-05-2017 08:51 63 a.js\r\n06-05-2017 16:32 120,232 a.pdf\r\n13-05-2017 20:49 spawnEXIT: 0
廣告