英文:
How to make nodejs to talk with golang
问题
我正在创建一个使用Node.js和Golang的Web应用程序。我需要将Node.js与连接到MongoDB并将数据返回给Node程序的Golang代码进行连接。有没有办法连接它们?我尝试使用gonode API。这是我使用gonode API的代码。
我的Node.js文件包含以下代码:
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();
});
这是我的gofile.go文件中的代码:
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
}
在终端中运行node node.js
时,我遇到了以下错误:
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)
答案1
得分: 7
从Golang 1.5开始,你可以将Go编译为共享对象二进制文件(*.so)。这样可以让你的Go编译库被Node.js、Python、Ruby、Java等其他语言调用。
这里有一个你可以参考的指南:https://medium.com/learning-the-go-programming-language/calling-go-functions-from-other-languages-4c7d8bcc69bf
英文:
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
答案2
得分: 5
感谢回复。我找到了一个解决方案。我创建了两个不同的服务器。一个用于NodeJS,另一个用于Golang。我在Node服务器中调用Golang的URI,并从Golang服务器获取数据。
英文:
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.
答案3
得分: 0
根据对gonode源代码的仔细检查,该模块似乎通过stdin/-out生成go代码作为子进程,并进行通信。EPIPE错误意味着另一端关闭了流。基于这一点,可能是您的go进程过早退出。
您可以尝试通过修改gonode/lib/command.js中的Command.prototype.execute来调试该问题,以打印发送给go进程的JSON数据。然后,您可以直接运行go程序,并通过stdin提供相同的输入来调试它。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论