2014-05-12 2 views
1

미들웨어에서 외부 세계의 일부 기능에 액세스하려면 어떻게해야합니까? 예를 들어 요청의 일부를 확인하고 locals을 설정해야하는 경우 해당 파일의 머리에서express.js 미들웨어의 외부 함수 사용

app.use(function (req, res, next) { 
    if (myfunc(req)) { // <-- how should I load this function to be accessible here? 
     res.locals.myvar = true; 
    } 
    next(); 
}); 

답변

3

사용자 정의 모듈을 가져올 것 : 파일처럼 보일 것이라고

var myfunc = require('./path/to/myfunc/module')

: 다음

var myfunc = function(req) { 
    // Do something. 
} 

module.exports = myfunc; 

, 당신은해야 전화하실 수 있습니다 : myfunc(req);

Custom NodeJS Modules.