Node.js 将参数传递给expressjs中的中间件函数
我正在尝试向nodejs添加缓存功能 我想要这样的代码Node.js 将参数传递给expressjs中的中间件函数,node.js,express,Node.js,Express,我正在尝试向nodejs添加缓存功能 我想要这样的代码 app.get('/basement/:id', cache, (req,res) => { client.set('basement' + req.params.id,'hello:'+req.params.id) res.send('success from source'); }); function cache(req,res,next) { console.log('Inside mycache:'
app.get('/basement/:id', cache, (req,res) => {
client.set('basement' + req.params.id,'hello:'+req.params.id)
res.send('success from source');
});
function cache(req,res,next) {
console.log('Inside mycache:' + req.params.id);
client.get('basement' + req.params.id, function (error, result) {
if (error) {
console.log(error);
throw error;
} else {
if(result !== null && result !== '') {
console.log('IN Cache, fetching from cache and returning it');
console.log('Result:' + result);
res.send('success from cache');
} else {
console.log('Not in Cache, so trying to fetch from source ');;
next();
}
}
});
}
app.get('/basement/:id', cache, (req,res) => {
client.set('basement' + req.params.id,'hello:'+req.params.id)
res.send('sucess from source');
});
我想对/base/:id路由接收的请求应用名为缓存的中间件函数
缓存函数将接收密钥作为其参数。
在缓存中,我想检查缓存是否存在,如果存在,则从那里返回,否则我想调用实际的路由处理程序。密钥基于一个或多个请求参数
这样,我将为应用程序中的每个处理程序编写一个单独的缓存函数
我的缓存函数中的逻辑是除键之外的通用逻辑,它基于实际的请求对象,并且可能因方法而异
所以,我想有一个通用的缓存函数,它可以把键作为参数,这样我就可以有这样的代码
app.get('/basement/:id', cache, (req,res) => {
client.set('basement' + req.params.id,'hello:'+req.params.id)
res.send('success from source');
});
function cache(req,res,next) {
console.log('Inside mycache:' + req.params.id);
client.get('basement' + req.params.id, function (error, result) {
if (error) {
console.log(error);
throw error;
} else {
if(result !== null && result !== '') {
console.log('IN Cache, fetching from cache and returning it');
console.log('Result:' + result);
res.send('success from cache');
} else {
console.log('Not in Cache, so trying to fetch from source ');;
next();
}
}
});
}
app.get('/basement/:id', cache, (req,res) => {
client.set('basement' + req.params.id,'hello:'+req.params.id)
res.send('sucess from source');
});
我的意思是我将把缓存密钥传递给缓存函数。因此,缓存函数可以是通用的
但是,如果我像下面那样更改缓存功能以接收缓存键,则它不起作用
function cache(cachekey,req,res,next) {
}
似乎我无法在缓存函数中使用其他参数来接收传递的参数
我想将缓存键作为参数传递给函数
如果有人遇到过类似的问题,你能在这方面帮助我吗
但是,如果我像下面那样更改缓存函数以接收
缓存密钥,它不工作
function cache(cachekey,req,res,next) {
}
不能,因为它不是有效的express中间件(实际上是一个错误中间件),express将按顺序传递:req
,res
,next
。对于错误中间件,err
,req
,res
,next
您的缓存函数
将需要返回一个中间件,这样您就可以向它传递一个密钥
我想创建一个通用的缓存函数,它可以接受任何缓存 钥匙在本例中,它是id,但其他情况下可能有不同的键 现在你可以这样使用它:
app.get('/basement/:id', cache('id', 'basement'), (req, res) => { /* ... */ });
app.get('/other/:foo', cache('foo', 'other'), (req, res) => { /* ... */ });
谢谢你在这方面的帮助。但是,我不确定如何传递缓存键,如果在这种情况下,缓存键是:id,我如何从我的app.get方法在缓存函数中传递它。当我执行以下操作时,我得到一个错误,app.get('/basic/:id',cache('basic'+req.params.id),(req,res)=>{^ReferenceError:req未定义如果键为:id,则正如您在函数中看到的那样,它出现在
req.params.id
中。您遇到了什么错误?我想创建一个可以接受任何缓存键的通用缓存函数。在这种情况下,它是id,但其他情况下可能有不同的键。因此,我希望o将密钥传递给缓存函数。在缓存函数中,如果我使用req.params.id,那么我就不能将该函数设置为通用函数,并将其用于其他场景。那么,只需编辑一点该函数。检查更新的答案。我的答案是你是如何做到这一点的,你现在没有给出太多关于你的函数应该具有多通用性的信息,但你可以调整一下你需要的参数。谢谢@MarcosCasagrande这正是我想要做的。