티스토리 뷰
Node.js / Express.js - How to override/intercept res.render function?
jihun202 2020. 7. 8. 11:34Old question, but found myself asking the same thing. How to intercept res render? Using express 4.0x something now.
You can use/write middleware. The concept was a bit daunting to me at first, but after some reading it made a little more sense. And just for some context for anyone else reading this, the motivation for overriding res.render was to provide global view variables. I want session to be available in all my templates without me having to type it in every res object.
The basic middleware format is.
app.use( function( req, res, next ) {
//....
next();
} );
The next param and function call are crucial to execution. next is the callback function, to allow multiple middleware to do their thing without blocking. For a better explanation read here
This can then be used to override render logic
app.use( function( req, res, next ) {
// grab reference of render
var _render = res.render;
// override logic
res.render = function( view, options, fn ) {
// do some custom logic
_.extend( options, {session: true} );
// continue with original render
_render.call( this, view, options, fn );
}
next();
} );'웹프로그래밍 > node.js & Typescript' 카테고리의 다른 글
| Ubuntu) node.js 업그레이드 안됨 해결 (0) | 2022.07.21 |
|---|---|
| Error: No valid exports main found for node_modules\colorette (0) | 2020.08.15 |
| 안전한 any 타입 만들기 (0) | 2020.02.11 |
| [node.js] require is not defined in typescript (0) | 2018.08.30 |
| [node.js] adding .css file to ejs (0) | 2016.05.16 |
- Total
- Today
- Yesterday
- 글로벌경제
- 증안펀드
- 국제정세
- 세금
- SK하이닉스
- 환율전망
- 반도체주
- 주식시황
- 트럼프관세
- 적금
- 부동산시장
- 코스피
- 시사정치
- 앱테크
- 주담대
- 사회이슈
- 사건사고
- 금융
- 외교안보
- 나스닥
- 재테크
- 금리인상
- 코스닥
- 원달러환율
- 부동산
- 내집마련
- 고금리
- 증시전망
- 부동산전망
- 환율
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 |
