dongyupan4850 2015-10-11 17:44
浏览 390
已采纳

从golang执行nodejs脚本(返回一个字符串,然后将该字符串传递回golang变量)的最佳方法是什么?

I'm currently doing it with os/exec and Stdout on golang's side, and console.log("string") on nodejs's side.

Basically I need to generate a string but can only do so within nodejs but the majority of my code is in golang, so I'm trying to make this little blip in my code as seamless, secure, and reliable as possible and I'm a little uneasy about resting such an important part of my program on "console.log" and reading from shell output.

In short: I'm wondering if there exists a better and more standard communication line between my node and go code then console.log + shell output, or is that perhaps optimal enough?

Oh and the function of this particular part of my program is to take a markdown text file and convert it to HTML using markdown-it.

Some ideas:

  • Communicate through HTTP (send the data/string to a golang http listener)
  • Communicate through the filesystem (write the string to a temporary file and read it with golang)
  • Communicate through "something similiar to HTTP but specific to local application data sharing"

P.S.

I can't use otto, since markdown-it doesn't run there.

Actual code:

parser.go

package main

import (
    "os"
    "os/exec"
    "fmt"
    "bytes"
)

func main() {
    cmd := "node"
    args := []string{"parser.js", "/home/user1/dev/current/wikis/Bob's Pain/markup/index.md"}
    process := exec.Command(cmd, args...)
    stdin, err := process.StdinPipe()
    if err != nil {
        fmt.Println(err)
    }
    defer stdin.Close()
    buf := new(bytes.Buffer) // THIS STORES THE NODEJS OUTPUT
    process.Stdout = buf
    process.Stderr = os.Stderr

    if err = process.Start(); err != nil {
        fmt.Println("An error occured: ", err) 
    }

    process.Wait()
    fmt.Println("Generated string:", buf)
}

parser.js

var md = require('markdown-it')();
var yaml = require('js-yaml');
var fs = require('fs');

if (process.argv.length < 3) {
  console.log('Usage: node ' + process.argv[1] + ' FILENAME');
  process.exit(1);
}

var filename = process.argv[2];
fs.readFile(filename, 'utf8', function(err, data) {
  if (err) {
    throw err;
  }
  parse(data)
});

function parse(data) {
    data = data.split("---")
    yamlData = data[1];
    markData = data[2];
    y = yamlProcess(yamlData);
    markData = "# "+y.title+"

"+markData
    html = markdownToHTML(markData);
    console.log(html) // SEND THE DATA BACK TO GOLANG
}

function yamlProcess(data) {
    try {
      var doc = yaml.safeLoad(data);
      return doc;
    } catch (e) {
      console.log(e);
      return {};
    }
}

function markdownToHTML(data) {
    return md.render(data);
}
  • 写回答

2条回答 默认 最新

  • douhuanchi6586 2015-10-12 10:55
    关注

    I've approached similar requirement both ways.

    For a build pipeline extension, I'd write a Python script that takes arguments from command line and outputs results to stdout. It's a simple interface, for a "run once", "everything succeeds otherwise fail fast" usage. If that's the same in your case, I'd keep the implementation as-is.

    For a web application, I had Java service for just a specific function (in my case, Natty date recognition from a natural language string). This has the benefit that the application is already "warmed up" at the time of the call, and will definitely respond faster rather than booting up each time the request comes in. Going with a rest interface will probably reveal more benefits over time - e.g. simpler client implementation, monitoring, deployment options, switch implementation without changing clients, etc. It's just more conventional this way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog