C节点本机扩展调用方法两次

所以我对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); 

当我打电话给我的网站时,我在terminal上看到以下内容

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

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

这是重复的。 这不是你的扩展中的错误,这是你的HTTP代码中的一个问题。

看到:

  • 简单的http服务器
  • node.js页面刷新调用资源两次?

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