PS: Please upgrade Node 6.2.1 first. The Node upgrade command npm install -gn;n stable.NOde.js extension is a dynamic link library written in C/C++ and loaded through the Node.js function require() function. It is like using an ordinary Node.js module. It mainly provides interfaces between Node and C/C++ libraries.
In this way, if a method or function is implemented through Node extension, it becomes quite complex, involving knowledge of several modules and interfaces:
•v8: A javascript.V8 that implements implementation through the C++ library provides an object creation mechanism, callback function, etc. Most of the V8API documents are in the v8.h header file. Click on my v8 online document
•libuv: A library for event loops that implements Node.js worker threads and asynchronous behavior. It also acts as a cross-platform abstract library that can simply POSIX-like access to all major operating system systems many common tasks, such as interaction with file systems, sockets, timers, and system events. libuv also provides an abstract pthreads-like thread that can be used for more complex asynchronous. Node.js' C/C++ extension needs to go beyond the standard event loop. The plug-in author encourages thinking about how to avoid blocking I/O event loops and complete task-intensive work through libuv non-blocking system operations, worker threads, and user-defined threads.
•Node.js built-in library: Node.js itself uses a large number of C/C++ extension APIs. The most important class for C/C++ extension node: ObjectWrap
•Node.js's numerous static link libraries, such as OpenSSL:Node.js's other libraries are in the deps directory under its source directory tree. For details, please see Node.js's own dependencies for additional information. Click on my official Node.js extension library example, which may be the starting point for you to write a C/C++ extension library for Node.js. Only V8 and OpenSSL classes are often used frequently in Node C/C++ extensions.
Node C/C++ extension first bomb - latest example Hello World
This example applies to Node.js version number V5.0 or above.
// hello.jsconst addon = require('./build/Release/addon');console.log(addon.hello()); // 'world'// hello.cc#include <node.h>#include <v8.h>namespace demo {using v8::FunctionCallbackInfo;using v8::Isolate;using v8::Local;using v8::Object;using v8::String;using v8::Value;void Method(const FunctionCallbackInfo<Value>& args) {Isolate* isolate = args.GetIsolate();args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));}void init(Local<Object> exports) {NODE_SET_METHOD(exports, "hello", Method);}NODE_MODULE(addon, init)} // namespace demo // binding.gyp{"targets": [{"target_name": "addon","sources": [ "hello.cc" ]}]}node-gyp command
The code copy is as follows:
node-gyp configure build
The above is the relevant knowledge of Node.js Addons translation (C/C++ extension) introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!