** node.js
- v8기반으로 동작하는 플랫폼.
- 자바스크립트 기반 언어
- 설치 (ubuntu 12.04기준)
apt-get install nodejs
apt-get install npm
** add-on
- c, c++ library(*.so파일들..)를 사용하기 위한 오브젝트
- node-gyp 설치 필요
- 설치 (ubuntu 12.04기준)
npm install -g mode-gyp
(python 2.7, make, c compiler like gcc설치 필요)
**add-on 예제
1. js file (test.js)
var binding = require('./build/Release/binding');
var timeVal = binding.time();
var loginVal = binding.login();
var http = require("http");
var server = http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<!DOCTYPE /html>");
response.write("<html>");
response.write("<head>");
response.write("<title>Hello World Page</title>");
response.write("</head>");
response.write("<body>");
response.write(timeVal);
response.write(loginVal);
response.write("</body>");
response.write("</html>");
response.end();
});
server.listen(8080, 'localhost');
console.log('binding.timeVal() =', timeVal);
console.log('binding.loginVal() =', loginVal);
2. binding.gyp - json타입이며, binding lib세팅, target_name이 library name
{
"targets": [
{
"target_name": "binding",
"sources": [ "binding.cpp" ]
}
]
}
3. binding.cpp
#include <node.h>
#include <v8.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
using namespace v8;
void getTime(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
time_t current_time;
time( ¤t_time);
args.GetReturnValue().Set(String::NewFromUtf8(isolate, ctime( ¤t_time)));
}
void getLogin(const FunctionCallbackInfo<Value>& args) {
int i = 0;
char cBuf[256];
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
int j = sprintf(cBuf, "%s", getlogin());
args.GetReturnValue().Set(String::NewFromUtf8(isolate, cBuf));
}
void init(Handle<Object> target) {
NODE_SET_METHOD(target, "time", getTime);
NODE_SET_METHOD(target, "login", getLogin);
}
NODE_MODULE(binding, init);
**build
$node-gyp configure build
**execute
$node test.js
**결과 화면
**sample code svn rep.
https://121.134.202.97/svn/Dev3/SRC/trunk/nodejs_addontest