扩展node.js的C ++ Hello world插件示例

时间:2014-07-17 01:35:57

标签: javascript c++ node.js

我终于设法从node.js文档中获取了命令行上的C ++ Hello world示例:http://nodejs.org/api/addons.html

当我运行节点hello.js'从命令行我获得预期的结果。但是,我考虑扩展这个简单的例子,而不是在命令行上打印一些东西,让字符串在网页上打印一些东西。

所以,我创建了一个index.html文件:

<!DOCTYPE html>
<html>
<head>

</head>

<body>

<p>When you click "Try it", a function will be called.</p>
<p>The function will display a message.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script src= ".hello.js"></script>

</body>
</html>

然后我将hello.js文件从链接中给出的内容更新为:

var addon = require('./build/Release/hello');

function myFunction() {
   document.getElementById("demo").innerHTML = addon.hello();
}

正如您所看到的,我的目标是,一旦点击按钮,它就会触发“我的功能”&#39;方法并在页面上打印一些东西。但是,它不起作用,我不确定我应该插入什么。

基本上,这个问题是关于如何通过node.js将数据从C ++函数传递到网页。

这是hello.cc文件:

#include <node.h>
#include <v8.h>

using namespace v8;

Handle<Value> Method(const Arguments& args) {
   HandleScope scope;
   return scope.Close(String::New("world"));
}

void init(Handle<Object> exports) {
  exports->Set(String::NewSymbol("hello"),
      FunctionTemplate::New(Method)->GetFunction());
}

NODE_MODULE(hello, init)

0 个答案:

没有答案