0

How can I import this code in module.exports ?

I'm pretty new in node js and in js. I want that this code could be used in other routes

 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()
    }
  }
}

How can I export it?

I was something like :

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()
        }
      }
    }

Also I try:

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()
    }
  }
}
}

But when I try to use it in a get request:

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

It brings:

TypeError: expCache.cache is not a function

Regards

1
  • 2
    for loop with git? Sorry, where is the git here? Commented Jan 23, 2017 at 22:33

2 Answers 2

1

You need to export an object, if you're expecting to be able to call expCache.cache:

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

However, if you want to keep your exported module as it is, just call it like this instead:

// 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) {
Sign up to request clarification or add additional context in comments.

1 Comment

I understand this. But it marks something in the = character: SyntaxError: Unexpected token =>
1

Try

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

You're exporting your cache function already, not an object containing it (which is how you try to use it for your router).

1 Comment

I tried that, but doesnt work... it doesn't gets through dthe file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.