Check out a free preview of the full Complete Intro to Databases course

The "Node.js App with Redis" Lesson is part of the full, Complete Intro to Databases course featured in this preview video. Here's what you'd learn in this lesson:

Brian builds a page counter Node.js application that uses a Redis database, and uses the promisify function to be able to use promises within the server instead of callbacks. Redis only understands callbacks.

Preview
Close

Transcript from the "Node.js App with Redis" Lesson

[00:00:00]
>> So I'm gonna come in here. I'm going to Node sample. I'm gonna get out of that, I'm gonna say mkdir redis-sample. I'm going to npm install. Let me get my versions out here. Npm init -y and I'm going to say npm install Reddis at 3.0.2 and express at 4.17.1.

[00:00:39]
So what are the interesting things about Redis? As far as I know, there's a company called Redis labs, Redis labs makes Redis. And they make some of the drivers and I don't think they make the node one. So I think this is actually a community driver and there's several, and I just picked node Redis because I think that's the most the largest one but there is also io Redis and there's a couple of other ones.

[00:00:59]
So this is not necessarily one that you have to use. This is just one that we're you and I are going to use together. Okay, we're going to make a directory called static, we're going to touch in static/index.html server.js. and cool. That should be fine. So we're going to say code with this.

[00:01:35]
Html:5. This will be our Redis sample. And here we're just going to implement like a page counter, which is going to be very exciting. We're gonna put an h1 here of visitors and we'll just put, loading here or something like that. And then here underneath that, we're gonna just have a script tag and we're gonna put an async function called register page view and then just underneath that we're going to actually call register paid view, okay, and here we're gonna say const rez equals.

[00:02:38]
await fetch('/pageview'), then we're gonna say const views. I'm going to do like this views to pull out the views key from this response equals await rez JSON and we're gonna say document get element by ID. Visitors. That's what we called it up there, right? Yep. Visitors. Enter text=views visitors all right, that's it.

[00:03:34]
I mean we basically just like implemented those like geo cities pageview counters. So I know everyone was dying to to know how to do that with Redis. And you're welcome, I think, is all I have to say. You spent so many hours working with me so that we could get to our culminating final project of Geocities page counters.

[00:03:58]
All right, cool that we're all here. Bet you're really glad you stuck this out all right, let's open our server digests. And just bring this magic home. So one of the really annoying things about this Redis driver is it does not speak promises. And I'm just not down with doing call backs anymore.

[00:04:18]
I don't know about you, but I spent too long writing Java Script to go back to call backs. So we're actually going to use promise defy to make these Redis methods work correctly. So I'm gonna say const promisify. This is actually built into node j s, which is very convenient, require util.

[00:04:38]
This will turn any like node style node back or callback, whatever you want to call those. Into a promise based API. And I'll show you how to do that here in just a second. Const Express equals require Express. Const Redis equals require Redis. And const client equals redis.createclient.

[00:05:09]
Okay. Well, we're going to make a new function here called Const Reddis incur. Right? So this is actually going to be a function. That's going to do a incur in our Redis server. And we're going to say equals promisifyclient.anchor. Right. So this is a method on this Redis client.

[00:05:34]
And then we're just going to say dot bind client. So, that this is correct. Otherwise this doesn't work. So now we get this function back. Called our anchor. And what all this does is it takes in a key and it'll do an increment in a bit. It works with promises now.

[00:05:50]
We don't have to do this we could totally do with node style call backs. I just wanted to show you how to make this a little bit easier. Okay, we're going to have an async function and knit and we're going to call a knit immediately after that. And I need this to be two spaces because otherwise I just can't even, okay.

[00:06:16]
Const app equals Express app dot get slash page views. page view. What did I say here? Page view. Okay, so page view with no assets. Async, req, res, Const views Equals await our inker and then we give it a key. So we're going to do it with page views.

[00:06:50]
So that this is the actual the name of the key that's going to go out to Redis. And then views will actually be the incremented number that's going to come back to us. Then we're gonna say res.json. Status okay? And views like that Okay, so now we need that'll work for our handler.

[00:07:20]
So underneath that, we just need to say port = 3000. app.use(express.static./static( and app.listen(PORT) and console.log Running on http://localhost. Colon port. Alright big moment here. See if we can get some incrementing numbers here that's being read and wrote to Redis. Let's see if we can get this running node server dot JS, running on local host, 300 one visitors, two visitors, three visitors.

[00:08:21]
We can just keep going all day. It's probably an already has more patriots than my blog. I don't have a blog. So it does. All right. So that at its most basic level is like how to connect your app to running to a Redis. Now, obviously it's very simple use case here, but I'm sure you could imagine.

[00:08:48]
You can do the same thing here with AR get an AR set and all that kind of stuff. So on and so forth. And you can do that all and you can just you don't even have to do this right you can use those note style call backs as well.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now