Tuesday 16 May 2017

Unable to connect with redis in app.get method

I am trying to learn use case of redis. As each tutorial is suggesting that it is better for caching a data.

I have made a simple demo where I am trying to connect with redis server in a web service get method.

var redis = require('redis');
var client = redis.createClient();
var express = require('express')
var app = express()

var port = process.env.PORT || 8080; // <== this is must

app.get('/fetch_offers', function (req, res) {
    client.on('connect', function() {
        console.log('connected');
    });
});

app.listen(port, function () {
  console.log(port)
})

I am trying to access it on localhost machine like http://localhost:8080/fetch_offers

I debugged it using console.log method but it does not print connected message. When I make this method outside the app.get... then it prints on executing node app.js.

I want it should make a redis connection on hitting a URL. I am not sure what is best way ? Can anyone help me ?

var redis = require('redis');
var client = redis.createClient();
var express = require('express')
var app = express()

var port = process.env.PORT || 8080; // <== this is must

client.on('connect', function() {
    console.log('connected');
});

app.get('/fetch_offers', function (req, res) {

});

app.listen(port, function () {
  console.log(port)
})

What wrong I am doing here ?



via Williams

No comments:

Post a Comment