Skip to content

Commit b53f9e5

Browse files
author
Ryan Kotzen
committed
Example of native modules.
1 parent e88de64 commit b53f9e5

File tree

10 files changed

+254
-75
lines changed

10 files changed

+254
-75
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ jspm_packages
3535

3636
# Optional REPL history
3737
.node_repl_history
38+
39+
node/native-modules/build/**

.idea/workspace.xml

+167-72
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@
22
A repository for my Beer & Technology talk on MongoDB and Node.js
33

44
# Further study
5-
Philip Roberts: What the heck is the event loop anyway? https://www.youtube.com/watch?v=8aGhZQkoFbQ
5+
Philip Roberts: What the heck is the event loop anyway? https://www.youtube.com/watch?v=8aGhZQkoFbQ
6+
7+
#TODO
8+
* npm global modules
9+
* native modules
10+
* edge.js
11+

node/2-npm/README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ Note that you can also install multiple dependencies at the same time. E.g.:
6767

6868
> e.g. `npm i moment lodash express`
6969
70-
#shrinkwrap
70+
This is all good and well, but how do I use a dependency once it has been installed?
71+
See index.js
7172

72-
#Anatomy of the project folder.
73+
#Shrinkwrap - make mine to go!
74+
75+
Once you are ready to deploy your app type `npm shrinkwrap` and this will freeze all of your dependencies, and their dependencies, and their dependencies, and their dependencies, etc and write it to the `npm-shrinkwrap.json` file in the root of your app. When your server hosting node does a `npm install`, this file will be used to install the exact dependencies you had on your dev/ci/cd box.

node/2-npm/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const moment = require("moment");
2+
const startTime = moment();
3+
console.log('Hello from %s!', startTime.fromNow()); //notice console.log by default uses util.format we played around with earlier.

node/native-modules/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Source
2+
See [Writing Native Node.js Modules](https://blog.risingstack.com/writing-native-node-js-modules/)
3+
4+
Windows: In an admin command prompt `npm i -g --production windows-build-tools`
5+
6+
Run the following from within the `native-modules` folder
7+
`node-gyp configure`
8+
`node-gyp build`

node/native-modules/binding.gyp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "addon",
5+
"sources": [ "example.cc" ]
6+
}
7+
]
8+
}

node/native-modules/example.cc

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <node.h>
2+
3+
const int maxValue = 10;
4+
int numberOfCalls = 0;
5+
6+
void WhoAmI(const v8::FunctionCallbackInfo<v8::Value>& args) {
7+
v8::Isolate* isolate = args.GetIsolate();
8+
auto message = v8::String::NewFromUtf8(isolate, "I'm a Node Hero!");
9+
args.GetReturnValue().Set(message);
10+
}
11+
12+
void Increment(const v8::FunctionCallbackInfo<v8::Value>& args) {
13+
v8::Isolate* isolate = args.GetIsolate();
14+
15+
if (!args[0]->IsNumber()) {
16+
isolate->ThrowException(v8::Exception::TypeError(
17+
v8::String::NewFromUtf8(isolate, "Argument must be a number")));
18+
return;
19+
}
20+
21+
double argsValue = args[0]->NumberValue();
22+
if (numberOfCalls + argsValue > maxValue) {
23+
isolate->ThrowException(v8::Exception::Error(
24+
v8::String::NewFromUtf8(isolate, "Counter went through the roof!")));
25+
return;
26+
}
27+
28+
numberOfCalls += argsValue;
29+
30+
auto currentNumberOfCalls =
31+
v8::Number::New(isolate, static_cast<double>(numberOfCalls));
32+
33+
args.GetReturnValue().Set(currentNumberOfCalls);
34+
}
35+
36+
void Initialize(v8::Local<v8::Object> exports) {
37+
NODE_SET_METHOD(exports, "whoami", WhoAmI);
38+
NODE_SET_METHOD(exports, "increment", Increment);
39+
}
40+
41+
NODE_MODULE(module_name, Initialize)

node/native-modules/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const myAddon = require('./build/Release/addon');
2+
console.log(myAddon.whoami());

npm-shrinkwrap.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)