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

如何使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 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题