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 + '...'));
Eventually it used to work - don't understand how and why - see my comment.