- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试向 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:' + 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();
}
}
});
}
我想将名为 cache 的中间件函数应用于 /basement/:id 路由收到的请求。
缓存函数将接收 key 作为其参数。在缓存中,我想检查缓存是否存在,如果存在则从那里返回它,否则我想调用实际的路由处理程序。 key 基于一个或多个请求参数。
这样,我最终将为我的应用程序中的每个处理程序编写一个单独的缓存函数。
我的缓存函数中的逻辑是对键的通用期望,它基于实际的请求对象,并且可能因方法而异。
所以,我想要一个可以将键作为参数的通用缓存函数,这样我就可以拥有这样的代码,
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) {
}
似乎我的缓存函数中不能有另一个参数来接收传递的参数。
我想将缓存键作为参数传递给函数。
如果有人遇到过类似的问题,你能帮我解决这个问题吗?
最佳答案
But, if I change my cache function like below as so as to receive the cache key, it does not work.
你不能因为它不是一个有效的 express 中间件(它实际上是一个错误中间件),express 将通过:req
, res
, next
按此顺序。和 err
, req
, res
, next
用于错误中间件。
您的缓存函数
将需要返回一个中间件,因此您可以将 key 传递给它。
I wanted to create a generic cache function which can take any cache key. In this case it was id, but other cases may have different keys
function cache(key, prefix = '') {
// Or arrange the parameters as you wish to suits your needs
// The important thing here is to return an express middleware
const cacheKey = prefix + req.params[key];
return (req, res, next) => {
console.log('Inside mycache:' + cacheKey);
client.get(cacheKey , function(error, result) {
if (error) {
console.log(error);
return next(error); // Must be an error object
}
if (result !== null && result !== '') {
console.log('IN Cache, fetching from cache and returning it');
console.log('Result:' + result);
return res.send('success from cache');
}
console.log('Not in Cache, so trying to fetch from source ');;
next();
});
}
}
现在你可以像这样使用它了:
app.get('/basement/:id', cache('id', 'basement'), (req, res) => { /* ... */ });
app.get('/other/:foo', cache('foo', 'other'), (req, res) => { /* ... */ });
关于node.js - 将参数传递给expressjs中的中间件函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50989393/
基于此答案的问题:https://stackoverflow.com/a/18650183/4478897 我试图找到这个解决方案,但似乎没有任何效果符合我的需要。 集群 expressjs 和 so
我有一个使用 Sequelize.js 作为 ORM 运行的快速应用程序。我的 express 应用程序从我的主要 Rails 应用程序接收请求,并且由于跨域策略,这些请求是使用 getJSON 执行
如何处理同步错误并保证在同一代码中出现错误? 这是我的代码,但不确定其工作正常,有什么想法吗? helpers.list({ limit: 1 })
在我的 ExpressJS 代码中,我限制主体大小如下: app.use(bodyParser.urlencoded({ limit : "512kb", extended: fals
我有一个看起来像这样的对象: editinvite: { _id: '', title: '', codes_list: [] } 问题是我无法在 NodeJS 应用程序中访问codes_
我正在尝试让我的nodeapp 使用express 工作。 我已经通过 nginx 设置了反向代理以使用 https://dank.ml/api/v1但每当我启动我的应用程序时,它都不想检测 / 响应
我完全失去了理智: 我有一个快速应用程序:以下是此示例的几个文件的快照: 应用程序.js -模型 --Event.js --Match.js 路线 --matches.js app.js: globa
我有我的nodejs项目,我想将记录器与我的route.js分开以获得干净的代码。 但是我遇到了下一个问题,当我尝试将记录器加载到route.js 文件时,它会显示下一个错误: TypeError:
我在 MEAN 应用程序中的模板缓存方面遇到一些困难。我有一个导航栏,它使用条件逻辑来显示/隐藏向用户显示的按钮。当用户点击页面时,这些值将被设置为 null 或 false,但是一旦他们登录,这些值
是否可以根据可选路线创建不同的查询? app.get('/:genre/:book?', function (req, res) { var genre = req.params.genre; v
我正在尝试学习 ExpressJS,并且遇到了这段代码。我似乎无法理解 app.use 函数,并且文档对我来说也不清楚。当调用 app.use 时,此特定示例代码中的/public 目录到底发生了什么
我正在开发一个学校管理系统。当老师发送发布请求时,我在后端创建一个路由内的全局对象。所以当多个老师访问服务器时,会创建多个全局对象......或者每个老师访问同一个对象?(会被重写同一个对象吗?) 最
我有一个用 typescript 编写的小型微服务,在 AKS 上的 kubernetes 集群中运行。我使用 Helm 生成了入口 apiVersion: extensions/v1beta1 ki
我需要将 JSON 发送到我的服务器,但 POST 请求显示为空正文。这是我在前端的 typescript : function sendData() : void { console.log("Se
这是我的 app.js import path from 'path'; import bodyParser from 'body-parser'; import express from 'expr
我一直在使用 ExpressJS 来促进文件上传,但据我所知,您可以将文件上传到任何路由,无论它是否处理它。 说我有 app.post('/photos/upload', photos.upload)
我正在阅读 Express.JS 4.x API,并且很好奇他们是如何设置的。这是我对正在发生的事情的理解:在 Express.JS 4.x API 的示例代码中,express 模块被导入并分配给变
我有以下注册页面 form(method='post', action='users/register') .form-group label Name inp
我有诸如 :id/comments/、:id/otherRoute 等路线。对于这些路线,我需要创建新的 Controller 。例如: let id: number = +req.params["i
嘿,我有一个使用 Express.js 和 Handlebars 运行的网站,该网站的导航栏和侧边栏在所有页面上都保持相同。当我路由时,它仅更改页面容器 {{{body}}}。 有没有一种方法可以让我
我是一名优秀的程序员,十分优秀!