Node.js Addons Translation (c + + extension) _node.js

Source: Internet
Author: User
Tags openssl

PS: Please upgrade the Node 6.2.1,node Upgrade command NPM install-g N;n stable first. Node.js extension is a dynamic link library written through c/s + +, and is loaded by Node.js function require () function, which is like using a common node.js module. It mainly provides interfaces between node and C + + library.

In this way, if a method or function is implemented through the node extension, it becomes quite complex, involving several modules and interface knowledge:

V8: A JavaScript implemented through the C + + library. V8 provides a mechanism for creating objects, callback functions, and so on. Most of the V8API documents are in the V8.h header file. Dot me V8 online documentation

LIBUV: A C Library of event loops for a platform that implements Node.js worker threads and asynchronous behavior. It also serves as a Cross-platform abstraction library for simply posix-like access to many common tasks of all major operating system systems, such as file system interactions, sockets, timers, and system events. LIBUV also provides an abstract pthreads-like thread that can be used for more complex asynchrony. Node.js-C + + extensions need to go beyond standard event loops. Plug-in authors encourage thinking about how to avoid blocking I/O event loops and accomplishing task-intensive work by LIBUV non-blocking system operations, worker threads, and user-defined threads.

Node.js Built-in Library: The Node.js itself uses a large number of C + + extended api,c/c++ extensions when one of the most important classes Node:objectwrap

node.js Numerous static link libraries such as OpenSSL:Node.js's other libraries under its source directory tree Deps directory. For more information, please see · Node.js ' s own dependencies for additional information. I node.js the official Extension Library sample, which is probably the starting point for you to write a C + + extensions library for Node.js. Only the V8 and OpenSSL classes are often used frequently in node C + + extensions.

Node + C + + expansion first bullet-The latest example Hello World

This example applies to node.js version number V5.0.

Hello.js
Const 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

Copy Code code as follows:

Node-gyp Configure Build

The above is a small set to introduce node.js addons translation (c + + extension) of the relevant knowledge, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.