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

标签 node.js unix couchdb

如果尚未启动,我正在尝试从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/

相关文章:

linux - 限制用户访问 linux/ubuntu 中的特定目录

linux - aix/ksh shell 前后的 grep 行

javascript - 使用 PhantomJS 从 CouchDB 获取文档

couchdb - CouchDB 真的可以用于桌面应用程序吗?

node.js - 如何在 graphql 中为基于 jwt 的身份验证实现自动刷新 token ?

javascript - Node.js 在函数之间异步传递变量

angularjs - 如何使用 Express 和 Angular 接收回发?

node.js - 当 props.match.params.id 返回未定义但 id 在 url 上可见时,如何获取 url 的 id 参数?

unix - shell 脚本算术运算

java - 如何处理继承?