Nood. js C ++ module addon plug-in (1)

Source: Internet
Author: User

For a person who has been familiar with C ++, it is a little difficult to suddenly turn to nodejs development. First of all, we need to understand what nodejs is. First, let's look at the name estimation as something related to javascript, in fact, it is only a javascript running environment, using Google V8 engine, using event-driven and asynchronous iO methods, is a new technology recently.

The company's requirement is to implement a uniform front-end and back-end specifications and use nodejs for rest, and then I am doing this part to implement c ++ encapsulation. I won't talk about what to install here, and google Baidu has everything.

First, the installation package can be directly used in windows and will be automatically configured. A directory such as zskycode contains the content

Then input in binding. gyp

{  'targets': [    {      'target_name': 'zskycode',      'sources': [        'src/hello.cc'],      'conditions': [        ['OS=="win"', {          'include_dirs': [              'deps/win32/include'          ],          'libraries': [              # TODO: fix node-gyp behavior that requires ../              # '../deps/win32/lib'          ]        }]              ]    }  ]}


Configure the source file library and Directory

Create package. json

{  "name": "zskycode",  "version": "0.0.4",  "author": "ZSkycode 
 
  ",  "description": "Sample package for Node.js",  "repository": {    "type": "git",    "url": ""  },  "homepage": "",  "main": "./lib/index.js",  "keywords": [  ],  "engines": {    "node": ">=0.6.14"  },  "dependencies": {  },  "scripts": {    "install": "node-gyp rebuild"  },  "devDependencies": {},  "optionalDependencies": {}}
 

You can also use npm init to input this information in a conversational manner.

Create the src file hello. cc mentioned in binding. gyp In the lib folder.

/** Create by Zork-zzg 2014/4/2 */# define BUILDING_NODE_EXTENSIONinclude
 
  
# Include
  
   
Using namespace v8; Handle
   
    
Method (const Arguments & args) {HandleScope scope; return scope. Close (String: New ("hello, world");} void init (Handle
    Exports) {exports-> Set (String: NewSymbol ("hello"), FunctionTemplate: New (Method)-> GetFunction ();} NODE_MODULE (zskycode, init)
Then run the command line to go to The zskycode folder directory and enter npm-gyp configure. If there is no error, the npm-gyp build will generate the node file you want under the release directory.

Then

Var hello = require ('./build/Release/hello. node'). hello (); console. log (hello); // print the world string here


In fact, you can directly create a project in the node_modules folder.

Var hello = require ('zskycode. hello (); console. log (hello); // print the world string here


It will execute the main method in the package. json file to call index. js.

var addon = require(__dirname + '/../build/Release/zskycode.node');module.exports = addon;



Of course, this example is just a simple helloworld. You can also release it. npm adduser creates an account and then npm publish releases it.

Then others can download it through npm install zskycode.

Here is the article.

Zookeeper

Related Article

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.