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

从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 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。