このコードを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.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()
}
}
}
:
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リクエストでそれを使用しよう:
var expCache = require('../../middleware/cache');
router.get('/:sid/fe',expCache.cache(3000),function(req,res) {
それがもたらす:
をTypeError: expCache.cache is not a function
よろしくあなたはexpCache.cache
を呼び出すことができるように期待している場合は、オブジェクトをエクスポートする必要が
* forループwith git *?申し訳ありません、ここでgitは何ですか? –