Javascript 导入节点js错误
如何在module.exports中导入此代码 我对node js和js都是新手。 我希望这个代码可以用于其他路线Javascript 导入节点js错误,javascript,node.js,Javascript,Node.js,如何在module.exports中导入此代码 我对node js和js都是新手。 我希望这个代码可以用于其他路线 cache = (duration) => { return (req, res, next) => { let key = '__express__' + req.originalUrl || req.url let cachedBody = mcache.get(key) if (cachedBody) { res.send
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
,则需要导出对象:
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) {
如果希望能够调用
expCache.cache
,则需要导出对象:
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) {
试一试
您已经在导出缓存函数,而不是包含它的对象(这是您尝试将其用于路由器的方式)。试试看
您已经在导出缓存函数,而不是包含它的对象(这就是您尝试在路由器中使用它的方式)。for loop with git?对不起,git在哪里?用于git循环?对不起,git在哪里?我试过了,但没用。。。它无法通过文件我试过了,但不起作用。。。文件没有通过,我明白。但是它在=character:SyntaxError:Unexpected token=>中标记了一些东西,我理解这一点。但它在=character:SyntaxError:Unexpected token=>