Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

348
Vistas
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 + '...')); 
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda