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