javascript - C Node native 扩展调用方法两次

标签 javascript c++ c node.js

所以我对 C 相当陌生,正在开发一个简单的 Node Native 扩展。

这是我的扩展程序 helloworld.c 的代码

Handle<Value> Method(const Arguments& args) {
  printf(":%s:\n", "Calling Method");
  //SendByte(bdrate,'1');
  HandleScope scope;
  if(toggleLight()==0){
    printf(":%s:\n", "Turning On");
    return scope.Close(String::New("Turned On"));
  }
  else{
printf(":%s:\n", "Turning Off");
return scope.Close(String::New("Turned Off"));
  }
}

void init(Handle<Object> target) {
  printf(":%s:\n", "Init");
  target->Set(String::NewSymbol("hello"),
      FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(helloworld, init)

我通过以下 Node.js 类使用前一个...

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

var http = require("http");

http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write(addon.hello());
  response.end();
}).listen(8888);

当我调用该网站时,我在终端中看到以下内容

~/Desktop/hellonode$ node testnode
:Init:
:Calling Method:
:Turning Off:
:Calling Method:
:Turning On:

为什么它似乎调用了两次该方法?我确信答案是显而易见的,但我看不到它。

最佳答案

这有点重复。这不是您的扩展程序中的错误,而是您的 HTTP 代码中的问题。

参见:

基本上,您的浏览器正在请求两个网址,//favicon.ico,并且由于您没有检查网址,因此它会在两个网址上运行您的扩展代码请求。

关于javascript - C Node native 扩展调用方法两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10164135/

相关文章:

javascript - 给element绑定(bind)onclick调用jquery中的方法

javascript - 在 Canvas 上绘制三 Angular 肌曲线

c++ - 如何实现线程向我的游戏添加计时器?

c++ - 我可以使用 c/c++ 与硬件设备通信吗?

c - 反转链表中的指针

c++ - 为什么在 C 和 C++ 的设计中没有对 CPU 状态寄存器的内在访问?

javascript - Bootstrap 切换不起作用?

javascript - 谷歌图表 API : Incorrect date being displayed

c++ - 从#define 转换为字符串 C++

c++ - 如何在不将输入和输出定向到标准输入的情况下使用 popen 在 C++ 中打开进程?