在node.js中使用Redis(Express) [英] Using redis with node.js (express)

查看:73
本文介绍了在node.js中使用Redis(Express)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习node.js(和表达框架)&这是有关redis&的基本新手问题.node.js.如何将Redis数据传递给模板?我应该在脚本中更正哪些内容,以便可以在模板中显示 teststring 的值?

I am learning node.js (and express framework) & here is a basic newbie question about redis & node.js. How to pass redis data to templates? What should I correct in my script, so I could display the value of teststring in a template?

app.get('/', function(req, res){
  res.render('index', {
    test: redisclient.get("teststring"),
  });
});

提前谢谢!

推荐答案

由于node.js模块(包括redis模块)往往是非阻塞且异步的,因此它们在回调中返回结果.尝试这种方式(我还建议您阅读关于异步代码和回调的这篇文章):

Since node.js modules (including the one for redis) tends to be non-blocking and asynchronous, they are returning results in callbacks. Try it this way (I also recommend to read this article regarding asynchronous code and callbacks):

app.get('/', function(req, res) {
  redisclient.get("teststring", function(error, response) {
    if(response) {
      res.render('index', {
        test: response,
      });
    } else {
      res.render('index', {
        test: error,
      });
    }
  });
});

这篇关于在node.js中使用Redis(Express)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆