index.js
This file
Path
examples/search/index.js
83 lines · 1.5 KiB · JavaScript
Last changed by
fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723
History of this fileTake it
The walk stopped after 200 commits: lines older than 2027b87 point at it.
fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87
lines 1–14why, one step back1'use strict'23// install redis first:4// https://redis.io/56// then:7// $ npm install redis8// $ redis-server910/**11 * Module dependencies.12 */1314var express = require('../..');fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
line 15why, one step back15var path = require('node:path');fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87
lines 16–18why, one step back16var redis = require('redis');1718var db = redis.createClient();fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723
line 19why, one step back19var app = express();fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87
line 20why, one step back20fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723
lines 21–22why, one step back21app.use(express.static(path.join(__dirname, 'public')));22fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87
lines 23–24why, one step back23// npm install redis24fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723
lines 25–27why, one step back25/**26 * Redis Initialization27 */fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87
line 28why, one step back28fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723
lines 29–31why, one step back29async function initializeRedis() {30 try {31 // connect to Redisfix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87
line 32why, one step back32fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723
line 33why, one step back33 await db.connect();fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87
line 34why, one step back34fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723
line 35why, one step back35 // populate searchfix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87
line 36why, one step back36fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723
lines 37–47why, one step back37 await db.sAdd('ferret', 'tobi');38 await db.sAdd('ferret', 'loki');39 await db.sAdd('ferret', 'jane');40 await db.sAdd('cat', 'manny');41 await db.sAdd('cat', 'luna');42 } catch (err) {43 console.error('Error initializing Redis:', err);44 process.exit(1);45 }46}47fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87
lines 48–51why, one step back48/**49 * GET search for :query.50 */51fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723
lines 52–59why, one step back52app.get('/search/{:query}', function (req, res, next) {53 var query = req.params.query || '';54 db.sMembers(query)55 .then((vals) => res.send(vals))56 .catch((err) => {57 console.error(`Redis error for query "${query}":`, err);58 next(err);59 });fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87
lines 60–72why, one step back60});6162/**63 * GET client javascript. Here we use sendFile()64 * because serving __dirname with the static() middleware65 * would also mean serving our server "index.js" and the "search.jade"66 * template.67 */6869app.get('/client.js', function(req, res){70 res.sendFile(path.join(__dirname, 'client.js'));71});72fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723
lines 73–83why, one step back73/**74 * Start the Server75 */7677(async () => {78 await initializeRedis();79 if (!module.parent) {80 app.listen(3000);81 console.log('Express started on port 3000');82 }83})();