2017-01-23 4 views
0

이 코드를 module.exports에서 가져올 수 있습니까?노드 js 오류 가져 오기

저는 노드 js와 js에서 꽤 새로 왔습니다. 이 코드는 내가 내보낼 수있는 방법 다른 경로

cache = (duration) => { 
    return (req, res, next) => { 
    let key = '__express__' + req.originalUrl || req.url 
    let cachedBody = mcache.get(key) 
    if (cachedBody) { 
     res.send(cachedBody) 
     return 
    } else { 
     res.sendResponse = res.send 
     res.send = (body) => { 
     mcache.put(key, body, duration * 1000); 
     res.sendResponse(body) 
     } 
     next() 
    } 
    } 
} 

에 사용될 수 있음을 원하는?

나는이었다 같은 :

module.export = { 
    cache: function(duration) { 
    return (req, res, next) => { 
    let key = '__express__' + req.originalUrl || req.url 
    let cachedBody = mcache.get(key) 
    if (cachedBody) { 
     res.send(cachedBody) 
     return 
    } else { 
     res.sendResponse = res.send 
     res.send = (body) => { 
     mcache.put(key, body, duration * 1000); 
     res.sendResponse(body) 
     } 
     next() 
    } 
    } 
} 
} 

을하지만 GET 요청에서 사용하려고 할 때 :

는 또한
module.exports = cache = (duration) => { 
     return (req, res, next) => { 
     let key = '__express__' + req.originalUrl || req.url 
     let cachedBody = mcache.get(key) 
     if (cachedBody) { 
      res.send(cachedBody) 
      return 
     } else { 
      res.sendResponse = res.send 
      res.send = (body) => { 
      mcache.put(key, body, duration * 1000); 
      res.sendResponse(body) 
      } 
      next() 
     } 
     } 
    } 

내가 시도

var expCache = require('../../middleware/cache'); 
    router.get('/:sid/fe',expCache.cache(3000),function(req,res) { 

그것은 제공 :

TypeError: expCache.cache is not a function 
당신이 expCache.cache 호출 할 수 기대하는 경우

안부

+2

* for for git *? 죄송합니다, 여기 자식은 어디 있습니까? –

답변

1

당신은 개체를 내 보내야합니다 : 당신이 그대로 내 보낸 모듈을 유지하려는 경우, 그러나

module.exports = { 
    cache: // your function 
} 

를 바로 호출 대신이 좋아 :

// inside middleware/cache: 
// module.exports = cache = (duration) => { 

var expCache = require('../../middleware/cache'); 

// you can call it as just a function 
router.get('/:sid/fe', expCache(3000), function(req,res) { 
+0

이 점을 이해합니다. 하지만 = 문자에 표시가되어 있습니다 : SyntaxError : 예기치 않은 토큰 => – arnoldssss

+0

mah 실수로 고마워 – arnoldssss

1

을 시도해보십시오

var expCache = require('../../middleware/cache'); 
router.get('/:sid/fe',expCache(3000),function(req,res) {.. 

캐시 기능을 포함하고있는 객체가 아닌 캐시 기능을 이미 내보내고 있습니다 (라우터에서 사용하는 방법).

+0

나는 그것을 시도했지만 나던 한 작업은 ... dthe 파일을 통과하지 못합니다. – arnoldssss

관련 문제