title: What's the optimal way to execute a nodejs script from golang, that returns a string, and then communicate that string back to a golang variable?
body: <p>I'm currently doing it with <code>os/exec</code> and <code>Stdout</code> on golang's side, and <code>console.log("string")</code> on nodejs's side.</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>Oh and the function of this particular part of my program is to take a markdown text file and convert it to HTML using <a href="https://github.com/markdown-it/markdown-it" rel="noreferrer">markdown-it</a>.</p>
  
  <p>Some ideas:</p>
  
  <ul>
  <li>Communicate through HTTP (send the data/string to a golang http listener)</li>
  <li>Communicate through the filesystem (write the string to a temporary file and read it with golang)</li>
  <li>Communicate through "something similiar to HTTP but specific to local application data sharing"</li>
  </ul>
  
  <p>P.S.</p>
  
  <p>I can't use otto, since markdown-it doesn't run there.</p>
  
  <p>Actual code:</p>
  
  <p><code>parser.go</code></p>
  
  <pre><code>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)
  }
  </code></pre>
  
  <p><code>parser.js</code></p>
  
  <pre><code>var md = require('markdown-it')();
  var yaml = require('js-yaml');
  var fs = require('fs');
  
  if (process.argv.length &lt; 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+"\n\n"+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);
  }
  </code></pre>