我在node.js和golang中创建了一个web应用程序。我需要将nodejs与golang代码连接起来,它与mongodb进行通信并将数据返回给节点程序。有什么方法可以连接吗?我试图使用gonode API。这是我使用gonode API的代码。如何使nodejs与golang交谈
我的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
}
这是我的错误,同时为节点节点运行越来越.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)
另外,go代码示例似乎与您的代码不同。在函数的开头,它调用response = make(gonode.CommandData),在if语句中,响应被赋值如下:response [“text”] =“What?”。不太了解go,但看起来你的变量m并不与任何东西挂钩。 https://www.npmjs.com/package/gonode – Juho