Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: search example to support Redis v4+ and Express 4/5 #6274

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions examples/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,41 @@ var db = redis.createClient();

// npm install redis

// connect to Redis

db.connect()
.catch((err) => console.error('Redis connection error:', err));

var app = express();

app.use(express.static(path.join(__dirname, 'public')));

// populate search

db.sadd('ferret', 'tobi');
db.sadd('ferret', 'loki');
db.sadd('ferret', 'jane');
db.sadd('cat', 'manny');
db.sadd('cat', 'luna');
(async () => {
try {
await db.sAdd('ferret', 'tobi');
await db.sAdd('ferret', 'loki');
await db.sAdd('ferret', 'jane');
await db.sAdd('cat', 'manny');
await db.sAdd('cat', 'luna');
} catch (err) {
console.error('Error populating Redis:', err);
}
})();
vinybk marked this conversation as resolved.
Show resolved Hide resolved

/**
* GET search for :query.
*/

app.get('/search/:query?', function(req, res, next){
var query = req.params.query;
db.smembers(query, function(err, vals){
if (err) return next(err);
res.send(vals);
});
app.get('/search/:query{0,1}', function (req, res, next) {
vinybk marked this conversation as resolved.
Show resolved Hide resolved
var query = req.params.query || '';
db.sMembers(query)
.then((vals) => res.send(vals))
.catch((err) => {
console.error(`Redis error for query "${query}":`, err);
next(err);
});
});

/**
Expand Down