最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make nodejs to talk with golang - Stack Overflow

programmeradmin1浏览0评论

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/jgranstrom/gonodepkg"
    json "github/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)

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)
Share Improve this question edited Nov 28, 2018 at 9:25 r0- 2,4981 gold badge25 silver badges29 bronze badges asked Jun 20, 2015 at 8:08 Murali ManoharMurali Manohar 3391 gold badge2 silver badges8 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 8

Golang from 1.5, you can build go to shared object binary file (*.so). This allows you to connect your go compiled library to be called by nodejs, python, ruby, java etc.

Here is a guide you could refer to: https://medium.com/learning-the-go-programming-language/calling-go-functions-from-other-languages-4c7d8bcc69bf

thanks for the response. I got a solution for this. I made 2 different servers. One for NodeJS and another for Golang. I am calling golang uri in Node server and getting data from golang server.

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.

发布评论

评论列表(0)

  1. 暂无评论