Skip to content
Myr

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 file

Take 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 back
1'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 back
15var path = require('node:path');

fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87

lines 16–18why, one step back
16var 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 back
19var app = express();

fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87

line 20why, one step back
20

fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723

lines 21–22why, one step back
21app.use(express.static(path.join(__dirname, 'public')));
22

fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87

lines 23–24why, one step back
23// npm install redis
24

fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723

lines 25–27why, one step back
25/**
26 * Redis Initialization27 */

fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87

line 28why, one step back
28

fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723

lines 29–31why, one step back
29async function initializeRedis() {
30 try {31 // connect to Redis

fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87

line 32why, one step back
32

fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723

line 33why, one step back
33 await db.connect();

fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87

line 34why, one step back
34

fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723

line 35why, one step back
35 // populate search

fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87

line 36why, one step back
36

fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723

lines 37–47why, one step back
37 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}47

fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87

lines 48–51why, one step back
48/**
49 * GET search for :query.50 */51

fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723

lines 52–59why, one step back
52app.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 back
60});
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});72

fix: search example to support Redis v4+ and Express 4/5 (#6274)Viny Brun Kriesangbefore adoptiond127723

lines 73–83why, one step back
73/**
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})();