gpt4 book ai didi

node.js - 通过exec或spawn从node.js启动couchdb

转载 作者:太空宇宙 更新时间:2023-11-03 22:03:03 25 4
gpt4 key购买 nike

如果尚未启动,我正在尝试从node.js 启动couchdb。如下代码适用于 pwd 等基本命令,但不适用于 couchdb:

var sys = require('util')
var exec = require('child_process').exec;
var child;

// executes `pwd`
child = exec("pwd", function (error, stdout, stderr) {
sys.print('stdout: ' + stdout);
sys.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});

我尝试使用“couchdb”和“/usr/local/bin/couchdb”作为 exec 的参数。

最佳答案

我现在有一个使用 CoffeeScript 的工作示例:

childproc = require "child_process"    
couchdb = childproc.spawn "couchdb"
couchdb.stdout.setEncoding "utf8"
buffer = ""

couchdb.stdout.on "data", (data) ->
lines = (buffer + data).split(/\r?\n/)
buffer = lines.pop()
lines.forEach (line, index) ->
console.log line

couchdb.stdout.on "end", ->
if buffer.length > 0
console.log buffer
buffer = ""
console.log 'process ended'

See my gist for a fuller example in CS, Iced CS & JS

编辑这是 Javascript 的输出:

var buffer, childproc, couchdb;

childproc = require("child_process");

couchdb = childproc.spawn("couchdb");

couchdb.stdout.setEncoding("utf8");

buffer = "";

couchdb.stdout.on("data", function(data) {
var lines;
lines = (buffer + data).split(/\r?\n/);
buffer = lines.pop();
return lines.forEach(function(line, index) {
return console.log(line);
});
});

couchdb.stdout.on("end", function() {
if (buffer.length > 0) {
console.log(buffer);
buffer = "";
}
return console.log('process ended');
});

关于node.js - 通过exec或spawn从node.js启动couchdb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11386613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com