C 节点本机扩展调用方法两次
C Node Native Extension calls method twice
所以我对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 代码中的问题。
看:
- 简单的 http 服务器
- 节点.js页面刷新调用资源两次?
基本上,您的浏览器正在请求两个URL,/
和/favicon.ico
,并且由于您没有检查URL,因此它会在两个请求上运行扩展代码。
相关文章:
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- g++的分段错误(在NaN上使用to_string两次时)
- 蛇在C++不会连续转两次
- 检查一个数组是否包含在另一个数组中,以相反的顺序,至少两次
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 我应该如何去缓解两次出现的cin?
- Realloc 两次无法在 Visual Studio 上运行
- 使用 getline(cin, var) 两次在进行字符串比较时会产生错误 (==)
- 为什么映射插入和 map.find() 的单次迭代比插入和 map.find() 的两次单独迭代慢得多
- C++析构函数调用两次,堆栈分配的复合对象
- 从工厂方法返回的ComPtr的引用计数增加两次
- 有什么方法可以通过删除表达式安全地处理两次释放内存?
- 执行随机开关函数的QT方式是什么连续两次使用相同情况的方法
- 为什么基类的构造函数方法被调用两次?
- C 节点本机扩展调用方法两次
- 在资源管理器左窗格上两次调用Windows 7外壳扩展dll Initialize方法
- Java方法调用两次从c++ JNI NDK函数
- c++方法链导致析构函数被调用两次
- 为什么map.insert()方法调用复制构造函数两次?
- (c++)调用一个curl方法两次,给出并出错