Node.js 算是目前最熱們的開發語言,所以我也開始探討未來應用在工作上的可能。

練習extensions "Hello world" module, Let's go.


 

實作環境

  • Ubuntu 12.04
  • Node.js v0.10.33

下載位置


 

  1. Create Helloworld folder ( 由於本人喜歡用資料夾去管理檔案,這樣比較一目了然)
    • cd /node-v0.10.33/src
    • mkdir -p helloworld
  2. 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

  3. 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);

  4. Added NODE_EXT_LIST_ITEM(node_hello) to src/node_extensions.h
  5. Modify node.gyp
    1. 在sources那邊,填入node_hello.c的位置,這樣compiler才會去編輯它
    • 'sources': [
      'src/helloworld/node_hello.cc',

  6. compiler node
    • make 
    • 未命名  
  7. execute node
    •   123  

有看到輸出 "Hello world, eason" 就大至上已經將helloworld module加入到node list 裡面了

最近要開始來惡補落後的進度了 :p

Enjoy it.

 

 

創作者介紹
創作者 I'm not 梵谷 的頭像
ps030010

I'm not 梵谷

ps030010 發表在 痞客邦 留言(0) 人氣( 14 )