Node.js 모듈화를 통한 유지보수의 용이성 높이기
모듈화 하기
nodeJs 프로그램 제작시 하나씩 모듈화 하여 import하는 것이 이후 유지보수에서 수고를 덜합니다.
아래는 그런 모듈 제작에 대한 간단한 예제들입니다.
제작된 모듈 불러오기
var modulename = require('./mymodule');// * 실제파일이름은 mymodule.js가 되어야 하며 "js"를 제외한 파일을 입력한다.
모듈로 제작하기
생성시 초기값이 있는 경우 (new를 이용하는 방법)
모듈
var self = module.exports = function(app){
var app = app;
this.method1 = function(){
},
this.method2 = function(){
this.method3();//내부 메소드 호출시
},
this.method3 = function(){
}
}
모듈호출
var _modulename = new modulename(app);
_modulename.method1
import 시 초기값을 넣어 주는 경우
module (mymodule.js)
module.exports = function(io, rClient) {
var foo = function (req, res, next) {
return ('foo');
}
var bar = function (req, res, next) {
return foo();
}
module.exports = {
foo: foo,
bar: bar
}
}
호출하기
var abc = require('./mymodule.js')(aa, bb); <!--() 없으면 undefined error
abc.foo
아래방법은 nodeJs에서는 동작하지 않는다.
module.exports = function(io, rClient) {
return {
get_picking_list: function(pick) { },
}
};<!- not work
생성시 인자값이 없는 경우
var self = module.exports = {
method1: function (obj) {
},
method2:function(){
self.method3();//내부 메소드 호출시
}
method3:function(){
}
}
modulename.method1(obj)
modulename.method2()
연관성 없는 호출
var self = module.exports = function(socket){
socket.on('getPickLadderData', function(req) {
get_data(req);
});
var get_data= function (req) {
}
}
한파일에 여러개의 export 노출 (사용시 new 없이 사용)
exports.sendBetresult = function (domain, obj) {
var test = function(){} <!-- 이렇게 정의하면 내부에서는 this없이 바로 사용
export.test = test
exports.testcall = function () {
test();
};
var gameRequest= require('./server/game.request');
gameRequest.sendBetresult('arg1', {});"
prototype 으로 정의
다른 변수에 인자값으로 넘겨 실행할때 유리
module
function History (gameTable) {
var self = this;
self.gameTable = new CBuffer(20);
gameTable.forEach(function(game) {
self.gameTable.push(game);
});
}
GameHistory.prototype.addCompletedGame = function (game) {
this.gameTable.unshift(game);
};
module.exports = History;
module 호출
var history = new History(results[0]);
require 인수 전달
var foo = function (req, res, next) {
return ('foo');
}
var bar = function (req, res, next) {
return foo();
}
Then:
module.exports = {
foo: foo,
bar: bar
}
var self = module.exports = {
foo: function (req, res, next) {
return ('foo');
},
bar: function(req, res, next) {
self.foo();
}
}
어떤 정의도 없이 바로 사용할 경우
var _this = this;
exports.somefunction = function() {
console.log('hello');
}
exports.someotherfunction = function() {
_this.somefunction();
}
var self = {
foo: function (req, res, next) {
return ('foo');
},
bar: function (req, res, next) {
return self.foo();
}
};
module.exports = self;
module.exports = function (app, db) {
var module = {};
module.auth = function (req, res) {
// This will be available 'outside'.
// Authy stuff that can be used outside...
};
// Other stuff...
module.pickle = function(cucumber, herbs, vinegar) {
// This will be available 'outside'.
// Pickling stuff...
};
function jarThemPickles(pickle, jar) {
// This will be NOT available 'outside'.
// Pickling stuff...
return pickleJar;
};
return module;
};
function Game(lastGameId, lastHash, lastSalt, gameHistory) {
function runGame() {}
}
Game.prototype.getInfo = function() { }
module.exports = Game;
module.exports = function (app, db) {
var module = {};
module.auth = function (req, res) {
// This will be available 'outside'.
// Authy stuff that can be used outside...
};
// Other stuff...
module.pickle = function(cucumber, herbs, vinegar) {
// This will be available 'outside'.
// Pickling stuff...
};
function jarThemPickles(pickle, jar) {
// This will be NOT available 'outside'.
// Pickling stuff...
return pickleJar;
};
return module;
};