doucheng1884 2015-06-20 08:08
浏览 157

如何使Node.js与Golang对话

I am creating a web app in node.js and golang. I need to connect nodejs with golang code which talks to mongodb and returns data to node program. is there any way to connect so? I tried to use gonode API.This is my code using gonode API.

my node.js file contains below code:

var Go = require('gonode').Go;
var options = {
path : 'gofile.go',
initAtOnce : true,
}

var go = new Go(options,function(err){
if(err) throw err;

go.execute({commandText: 'Hello world from gonode!'}, function(result, response) {
        if(result.ok) {
            console.log('Go responded: ' + response.responseText);
        }
});

go.close();
});     `

And this is the code in my gofile.go file:

package main

import(
    gonode "github.com/jgranstrom/gonodepkg"
    json "github.com/jgranstrom/go-simplejson"
)

func main(){
    gonode.Start(process)
}

func process(cmd *json.Json) (response *json.Json) {    
    response, m := json.MakeMap()

    if(cmd.Get("commandText").MustString() == "Hello") {
        m["responseText"] = "Well hello there!"
    } else {
        m["responseText"] = "What?"
    }

    return
}

This is the error am getting while running as node node.js in terminal

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: write EPIPE
    at errnoException (net.js:905:11)
    at Object.afterWrite (net.js:721:19)
  • 写回答

3条回答

  • dongzhi9032 2015-06-20 08:42
    关注

    Based on a very cursive check of the gonode source code, the module seems to spawn go code as a child process and communicate through stdin/-out. EPIPE error means that the other end closed the stream. Based on this it might be that your go process exits prematurely.

    You could try to debug the problem by modifying Command.prototype.execute in gonode/lib/command.js to print out the JSON that's sent to the go process. Then you can debug the go program by running it directly and giving it the same input via stdin.

    评论

报告相同问题?

悬赏问题

  • ¥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