2016-07-19 4 views
1

원래 app.get이 호출되기 전에 몇 가지 작업을 수행하려고합니다.Expressjs ejs fn.apply 오류 '보기가 생성자가 아닙니다'

this 페이지를 찾았는데, 내가 한 일을 시도해 보았습니다. 그리고 대부분은 효과적이었습니다. 렌더링 엔진을 사용하려고 할 때 기대했습니다.

내가 (appexpress()의 결과입니다) 사용하고 코드 : TypeError: View is not a constructor

사람이 내가이 문제를 해결할 수있는 방법을 알고 있나요 :

const express = require('express'); 
const app = express(); 
const ejs = require('ejs'); 
app.set('view engine', 'ejs'); 

var originalfoo = app.get; 
app.get = function() { 
    // Do stuff before calling function 
    console.log(arguments); 
    // Call the function as it would have been called normally: 
    originalfoo.apply(this, arguments); 
    // Run stuff after, here. 
}; 

app.get('/home', function (req, res) { 
    //res.send('hello world') // This works 
    res.render('index'); // This crashes 
}); 

res.render 나를이 오류를 준다?

PS : /views/index.ejs

답변

2
당신은 단지 원래의 함수 호출을 반환해야

을 존재하지, 그렇지 않으면 장식 app.get 방법은 더 이상 원래는 달리 아무 것도 반환하지 않습니다 :

참으로 속임수를 썼는지
app.get = function() { 
    // Call the function as it would have been called normally: 
    return originalfoo.apply(this, arguments); 
}; 
+0

! 대단히 감사합니다 !! – CreasolDev

관련 문제