'Nodejs' 카테고리의 다른 글
NPMv3 사용하기 (0) | 2016.04.04 |
---|---|
Node.js와 ECMAScript 2015 (ES6) (0) | 2016.01.04 |
Node.js NPM Previous Version 설치 하기 (0) | 2015.12.08 |
play.node(2015) 후기 (0) | 2015.11.13 |
Nodejs Express 4.x File Upload 설정 하기 (0) | 2015.02.10 |
NPMv3 사용하기 (0) | 2016.04.04 |
---|---|
Node.js와 ECMAScript 2015 (ES6) (0) | 2016.01.04 |
Node.js NPM Previous Version 설치 하기 (0) | 2015.12.08 |
play.node(2015) 후기 (0) | 2015.11.13 |
Nodejs Express 4.x File Upload 설정 하기 (0) | 2015.02.10 |
Node.js와 ECMAScript 2015 (ES6) (0) | 2016.01.04 |
---|---|
Node.js PM2 로 Debug Log 볼 때 stderr 에 출력되는 현상 해결 하기 (0) | 2015.12.08 |
play.node(2015) 후기 (0) | 2015.11.13 |
Nodejs Express 4.x File Upload 설정 하기 (0) | 2015.02.10 |
NVM(Node Version Manager) 를 사용하여 Nodejs 설치 하기 (0) | 2014.11.26 |
Node.js PM2 로 Debug Log 볼 때 stderr 에 출력되는 현상 해결 하기 (0) | 2015.12.08 |
---|---|
Node.js NPM Previous Version 설치 하기 (0) | 2015.12.08 |
Nodejs Express 4.x File Upload 설정 하기 (0) | 2015.02.10 |
NVM(Node Version Manager) 를 사용하여 Nodejs 설치 하기 (0) | 2014.11.26 |
Nodejs sudo npm command not found 해결 방법 (0) | 2013.12.22 |
Node.js NPM Previous Version 설치 하기 (0) | 2015.12.08 |
---|---|
play.node(2015) 후기 (0) | 2015.11.13 |
NVM(Node Version Manager) 를 사용하여 Nodejs 설치 하기 (0) | 2014.11.26 |
Nodejs sudo npm command not found 해결 방법 (0) | 2013.12.22 |
Node.js 10가지 놀라운 프로젝트 (0) | 2013.12.21 |
play.node(2015) 후기 (0) | 2015.11.13 |
---|---|
Nodejs Express 4.x File Upload 설정 하기 (0) | 2015.02.10 |
Nodejs sudo npm command not found 해결 방법 (0) | 2013.12.22 |
Node.js 10가지 놀라운 프로젝트 (0) | 2013.12.21 |
Nodejs Express Middleware 사용하기 (0) | 2013.11.12 |
npm global 설치 시 주로 발생 한다.
centos 기준 /usr/local/lib/node_modules 폴더 접근 권한이 없기 때문에 root 권한을 줘야 한다.
sudo npm install -g [package name]
root 에서는 node 명령어를 못찾는 경우가 있다. 그럴 경우
sudo ln -s /usr/local/bin/node /usr/bin/node sudo ln -s /usr/local/lib/node /usr/lib/node sudo ln -s /usr/local/bin/npm /usr/bin/npm
위 명령어를 추가 한 후 사용 하자.
Nodejs Express 4.x File Upload 설정 하기 (0) | 2015.02.10 |
---|---|
NVM(Node Version Manager) 를 사용하여 Nodejs 설치 하기 (0) | 2014.11.26 |
Node.js 10가지 놀라운 프로젝트 (0) | 2013.12.21 |
Nodejs Express Middleware 사용하기 (0) | 2013.11.12 |
Nodejs Memory 관리 (0) | 2013.11.11 |
Node.js 요즘에 주목 받고 있는 언어 같다.
Node.js 는 자바스크립트, 구글 V8 엔진을 사용 하는데, 자바스크립트는 개발자들에게 익숙한 언어이고, 웹 애플리케이션 서버 개발시 직접 구현도 가능 하지만 Expressjs 라는 대표적인 웹프레임워크를 많이 사용 하는 편이다.
전화통화로 몇일 전 위 이유 때문에 많이 사용 하지 않을까? 라고 말했던 형도 있었다.
Express 공식 사이트
여기서 한가지 짚고 넘어갈 것이 있다.
Node 공식 사이트 소개 글
Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
위 내용을 보자면 쉽게 확장이 가능한 네트워크 애플리케이션을 구축 하기 위한 플렛폼이고, 추가적인 기능으로 이벤트 기반, non-blocking I/O 를 지원 한다.
웹프레임워크로 상대적으로 쉽게 개발을 하기 때문에 망각 할 수 있지만, 기억하고 있어야 할 부분이라고 본다.
Node.js 기반으로 만들어진 놀라운 10가지 프로젝트가 있다. 그냥 읽고만 넘기기엔 내용이 좋다고 판단이 되어 글을 남겨 본다.
이거 정말 필요할 것 같다. 라고 생각 된건 두 가지!!
실제 개발이 완료 된 후 운영에 들어간다면 데스크톱 애플리케이션 처럼 사용하면서 정보를 가공할수 있는 Node-Webkit, 서버 로그 관리 차원에서 훌륭한 도구가 될 Log.io 보고 와~ 했다.
[그림 1] log.io 공식 페이지
틈틈히 한번씩을 사용해봐야 겠다. 먼가 도움이 많이 될거 같다.
※ 참고 사이트
- http://www.itworld.co.kr/slideshow/85221
NVM(Node Version Manager) 를 사용하여 Nodejs 설치 하기 (0) | 2014.11.26 |
---|---|
Nodejs sudo npm command not found 해결 방법 (0) | 2013.12.22 |
Nodejs Express Middleware 사용하기 (0) | 2013.11.12 |
Nodejs Memory 관리 (0) | 2013.11.11 |
Express req.param 사용 하기 (0) | 2013.09.23 |
Express middleware는 Java 진영의 Springframework의 interceptor, aop 와 비슷한 성향을 가지고 있습니다.
위 그림을 참고 해보자.
서버로 요청이 들어오면 middleware 는 route / 모든 주소에 대해서 처리하라는 지시를 받습니다. 그 후 특정 url 에 대해서 처리를 한 후 응답을 하게 됩니다.
실제로 log를 확인해 봤습니다.
app object 중 stack
stack: [ { route: '', handle: [Function: query] }, { route: '', handle: [Function: expressInit] }, { route: '', handle: [Function: logger] }, { route: '', handle: [Function: cookieParser] }, { route: '', handle: [Function: bodyParser] }, { route: '', handle: [Function: methodOverride] }, { route: '', handle: [Function: test] }, { route: '', handle: [Function: session] }, { route: '', handle: [Function] }, { route: '', handle: [Function] }, { route: '', handle: [Function: router] }, { route: '', handle: [Function: errorHandler] } ]
app object 중 router
_router: { map: { get: [ { path: '/test/test', method: 'get', callbacks: [ [Function] ], keys: [], regexp: /^\/test\/test\/?$/i }, .... ], post: [ { path: '/test/test', method: 'get', callbacks: [ [Function] ], keys: [], regexp: /^\/test\/test\/?$/i }, .... ] } }
서버로 request가 들어오는 이벤트가 발생할 경우 stack 을 통과하게 됩니다.
개발자가 직접 정의 하는 middleware 는 test 라는 함수이고, 모든 url 처리는 stack 를 통과하고 특정 url은 router 에게 처리를 위임 하게 됩니다. 실제 code를 보도록 하자.
./express/lib/application.js 중 일부 입니다.
/** * Proxy `connect#use()` to apply settings to * mounted applications. * * @param {String|Function|Server} route * @param {Function|Server} fn * @return {app} for chaining * @api public */ app.use = function(route, fn){ var app; // default route to '/' if ('string' != typeof route) fn = route, route = '/'; // express app if (fn.handle && fn.set) app = fn; // restore .app property on req and res if (app) { app.route = route; fn = function(req, res, next) { var orig = req.app; app.handle(req, res, function(err){ req.app = res.app = orig; req.__proto__ = orig.request; res.__proto__ = orig.response; next(err); }); }; } connect.proto.use.call(this, route, fn); // mounted an app if (app) { app.parent = this; app.emit('mount', this); } return this; };
"default route to '/'" 내부적으로 기본 값으로 설정이 되는 것을 확인 할 수 있습니다.
이제 실제 사용법을 알아보도록 하자.
http://expressjs.com/api.html#app.use 공식 홈페이지 Reference 자료 입니다.
두번째 인자로 function 이 사용 됩니다.
위 자료를 참고 하여 Sample Code 작성 해보자.
함수형으로 return 하자.
function test() { return function test(req, res, next) { console.log('test'); next(); } }
next() 는 stack 의 다음 주소로 가라는 함수 입니다. 실제로 next() 를 호출 하지 않는 경우 응답을 하지 못하고 서버에서 멈춰버리게 됩니다.
app.js 설정의 일부 입니다. 실제로 적용 하자.
function setting() { app.configure(function(){ app.set('port', process.env.PORT); app.use(express.logger('dev')); app.use(express.cookieParser()); app.use(express.bodyParser()); app.use(express.methodOverride()); // user middleware app.use(test()); // session config app.use(express.session({ secret: config.redis.secret, store: new RedisStore(config.options), cookie: config.cookie })); app.use(passport.initialize()); app.use(passport.session()); // router app.use(app.router); }); }
use 함수 호출 하는 부분을 볼 수 있는데, 해당 행위는 모든 요청에 대해서 처리를 하겠다는 말입니다.
위와 같이 적용 하면 모든 요청에 대해서 test 함수가 호출 되는 것을 확인 할 수 있습니다.
※ 참고 사이트: http://expressjs.com/
Nodejs sudo npm command not found 해결 방법 (0) | 2013.12.22 |
---|---|
Node.js 10가지 놀라운 프로젝트 (0) | 2013.12.21 |
Nodejs Memory 관리 (0) | 2013.11.11 |
Express req.param 사용 하기 (0) | 2013.09.23 |
Nodejs winston 표준 시간이 안맞는 경우 해결 방법 (0) | 2013.09.23 |