mirror of
https://github.com/expressjs/express.git
synced 2026-02-22 03:51:36 +00:00
fix: search example to support Redis v4+ and Express 4/5 (#6274)
* Fix Redis example to support Redis v4+ and Express 4/5
* update optional route syntax to /{:query} and refactor Redis initialization into dedicated function to guarantee that it is complete before server starts
---------
Co-authored-by: Sebastian Beltran <bjohansebas@gmail.com>
This commit is contained in:
committed by
GitHub
parent
6b7ccfcf12
commit
d12772393c
@@ -16,31 +16,47 @@ var path = require('node:path');
|
|||||||
var redis = require('redis');
|
var redis = require('redis');
|
||||||
|
|
||||||
var db = redis.createClient();
|
var db = redis.createClient();
|
||||||
|
|
||||||
// npm install redis
|
|
||||||
|
|
||||||
var app = express();
|
var app = express();
|
||||||
|
|
||||||
app.use(express.static(path.join(__dirname, 'public')));
|
app.use(express.static(path.join(__dirname, 'public')));
|
||||||
|
|
||||||
// populate search
|
// npm install redis
|
||||||
|
|
||||||
db.sadd('ferret', 'tobi');
|
/**
|
||||||
db.sadd('ferret', 'loki');
|
* Redis Initialization
|
||||||
db.sadd('ferret', 'jane');
|
*/
|
||||||
db.sadd('cat', 'manny');
|
|
||||||
db.sadd('cat', 'luna');
|
async function initializeRedis() {
|
||||||
|
try {
|
||||||
|
// connect to Redis
|
||||||
|
|
||||||
|
await db.connect();
|
||||||
|
|
||||||
|
// populate search
|
||||||
|
|
||||||
|
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 initializing Redis:', err);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET search for :query.
|
* GET search for :query.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
app.get('/search/:query?', function(req, res, next){
|
app.get('/search/{:query}', function (req, res, next) {
|
||||||
var query = req.params.query;
|
var query = req.params.query || '';
|
||||||
db.smembers(query, function(err, vals){
|
db.sMembers(query)
|
||||||
if (err) return next(err);
|
.then((vals) => res.send(vals))
|
||||||
res.send(vals);
|
.catch((err) => {
|
||||||
});
|
console.error(`Redis error for query "${query}":`, err);
|
||||||
|
next(err);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -54,8 +70,14 @@ app.get('/client.js', function(req, res){
|
|||||||
res.sendFile(path.join(__dirname, 'client.js'));
|
res.sendFile(path.join(__dirname, 'client.js'));
|
||||||
});
|
});
|
||||||
|
|
||||||
/* istanbul ignore next */
|
/**
|
||||||
if (!module.parent) {
|
* Start the Server
|
||||||
app.listen(3000);
|
*/
|
||||||
console.log('Express started on port 3000');
|
|
||||||
}
|
(async () => {
|
||||||
|
await initializeRedis();
|
||||||
|
if (!module.parent) {
|
||||||
|
app.listen(3000);
|
||||||
|
console.log('Express started on port 3000');
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user