• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

257
Views
Why combining Node Express and Redis can't set a simple Key-Value pair?

Basically, the below put service that should execute a simple Redis-cli> SET KEY VALUE can't work. The get operations work well. Using separately the redis module and calling the set function also works. But when called from app.put() the KEY/VALUE pair isn't registered.

What's the hell???

// express setup
const REDIS_REST_PORT = 3000;
const express = require('express');
const router = express.Router();
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());

// redis setup
const REDIS_CONNECTION_STRING = "redis://127.0.0.1:6379";
const RedisCli = require('redis').createClient(REDIS_CONNECTION_STRING);
RedisCli.on('connect', function() {
    console.log('Connected to REDIS');
});
RedisCli.on('error', function(err) {
    console.log('/!\ REDIS ERROR: ' + err);
});

// GET .../get/KEY (works well !!)
app.get('/get/:key', function(req, res) {
    RedisCli.get( req.params.key, function (err, result) {
        if (err) {
            res.send(err,500);
        } else {
            res.send(result);
        }    
    });
});

// PUT .../set/KEY + body (can't work KEY/VALUE never registered ??)
app.put('/set/:key', function(req, res) {
    var value = "'" + JSON.stringify(req.body) + "'";
    console.log("SET " + req.params.key + " " + value);
    RedisCli.set( req.params.key, value,
        function (err, result) { 
            if (err) {
                res.send(err,500);
            } else {
                res.send(result);
            }    
    });
});

// Start REST server
app.listen(REDIS_REST_PORT, () =>
    console.log('Listening on port '+ REDIS_REST_PORT + '...')); 
almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Eventually it used to work - don't understand how and why - see my comment.

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error