Mur*_*har 5 javascript go mongodb node.js
我正在 node.js 和 golang 中创建一个 web 应用程序。我需要将 nodejs 与 golang 代码连接起来,golang 代码会与 mongodb 对话并将数据返回给 node 程序。有什么办法可以连接吗?我尝试使用 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();
}); `
Run Code Online (Sandbox Code Playgroud)
这是我的 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
}
Run Code Online (Sandbox Code Playgroud)
这是在终端中作为节点 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)
Run Code Online (Sandbox Code Playgroud)
感谢您的回复。我对此有一个解决方案。我做了两个不同的服务器。一个用于 NodeJS,另一个用于 Golang。我在 Node 服务器中调用 golang uri 并从 golang 服务器获取数据。
Golang 从 1.5 开始,您可以构建转到共享对象二进制文件 (*.so)。这允许你连接你的 go 编译库以被 nodejs、python、ruby、java 等调用。
这是您可以参考的指南:https : //medium.com/learning-the-go-programming-language/calling-go-functions-from-other-languages-4c7d8bcc69bf
小智 0
基于对 gonode 源代码的粗略检查,该模块似乎将 go 代码作为子进程生成,并通过 stdin/-out 进行通信。EPIPE错误意味着另一端关闭了流。基于此,您的 go 进程可能会提前退出。
您可以尝试通过修改 gonode/lib/command.js 中的 Command.prototype.execute 来调试问题,以打印出发送到 go 进程的 JSON。然后,您可以通过直接运行 go 程序并通过 stdin 为其提供相同的输入来调试该程序。
| 归档时间: |
|
| 查看次数: |
9385 次 |
| 最近记录: |