Node.js 算是目前最熱們的開發語言,所以我也開始探討未來應用在工作上的可能。
練習extensions "Hello world" module, Let's go.
實作環境
- Ubuntu 12.04
- Node.js v0.10.33
下載位置
- node.js https://nodejs.org/download/
- Create Helloworld folder ( 由於本人喜歡用資料夾去管理檔案,這樣比較一目了然)
- cd /node-v0.10.33/src
- mkdir -p helloworld
- Create a file node_hello.h
-
#ifndef NODE_HELLO_H_
#define NODE_HELLO_H_#include <v8.h>
namespace node {
v8::Handle<v8::Value> Hello(const v8::Arguments& args);
}#endif
- Create a file node_hello.cc
-
#include <node.h>
#include "node_hello.h"
#include <v8.h>namespace node {
using namespace v8;
Handle<Value> Method(const Arguments& args){
HandleScope scope;
return scope.Close(String::New("Hello world, eason"));
}void init(Handle<Object> target){
target->Set(String::NewSymbol("sayhello"),
FunctionTemplate::New(Method)->GetFunction());
}}
NODE_MODULE(node_hello, node::init); - Added NODE_EXT_LIST_ITEM(node_hello) to src/node_extensions.h
- Modify node.gyp
- 在sources那邊,填入node_hello.c的位置,這樣compiler才會去編輯它
-
'sources': [
'src/helloworld/node_hello.cc', - compiler node
- make
- execute node
-
有看到輸出 "Hello world, eason" 就大至上已經將helloworld module加入到node list 裡面了
最近要開始來惡補落後的進度了 :p
Enjoy it.
請先 登入 以發表留言。