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