- [Instructor] In our shopping application,
we are going to use redis to implement first sessions
and then also the shopping basket.
But before we can do all that,
we have to first bring in redis and configure it.
So for that, please make sure
that you are in the shopper directory again.
And here, I now run NPM install ioredis.
And to set up redis and to establish the connection,
we will again, use serverbinstartJS
and there I will first,
after mongoose add const redis=requireioredis.
Next, I switch really quick into the config file,
and here in indexJS, I will now add a new property,
redis aftermongoDB, because we also want to configure redis.
And in our case, we first just need the port,
and the port is 7379.
So that's the port of our docker container.
And I also add one more property, that's client,
and it should be null.
And this will make sense to you really soon.
First, I head back now to startJS,
and in there, I will now add a new function,
after this connect to mongoose function.
And I call it function connecttoredis.
And as I said before,
this has not to be an asynchronous function
because ioredis will easily connect to the redis server.
So no promises returned.
I can now simply add const redis=new redis
and there are passing config.redis.port,
If we would use the default port
we would not even need this argument.
Next it also wants to be notified once redis connects.
And in this case I have to add redis on.
So this is now an event handler
and the event we are listening to is connect.
And if disconnect event fires
we want to log something out on the console.
So I add console.info.
And there, I print out successfully connected to Redis.
And I also want to add an error handler.
So if something goes wrong
on the Redis server, and I'll add redis on error,
and this event has an argument, the error and then
I will simply console.error,
and I will print out this error
that was passed in to this event,
and also ask for Mongo DB and mongoose before,
if this happens I just want this application to crash.
Because I don't want this application to start to, to run,
if the redis connection is not established.
So in this case, I now add process exit
with the argument one, which means there was an error.
And in the very end,
let's not forget to return redis from that.
So that's now the redis client
that holds the prepared connection,
and that will be then used to actually connect
to the redis server.
Next, right after that,
I will now already use this function.
So I add const redis = connecttoredis.
I do this right here,
because our main application will need redis
right from the start.
And if we scroll down a little bit
we see that we are running our main application
our appJS file here in line 36.
Differently then, with mongoose
where the Mongo DB connection is automatically available
through the model, for redis we have to find a way
to make the client available to the whole application.
And for that, we will use the configuration object
because I wrote the application in a way
so that the conflict is passed
to the main application and also to all routes.
So now I will store redis in my config object.
And for that I will now add config.redis.
And now this is the client
that was set to null before, and assign redis to it.
And now as you see in line 37,
when we instantiate express, we pass already
the config in and this cofig will already
then contain the redis client.
Now let's try this out.
So I run npm run dev, and you see that I get the message
that we successfully connected to redis.
I also want to try out what happens
if there is a problem about connecting.
For that, I just go into the config indexjs file
and let's just change the port.
So I change it to 7370 zero.
I save, the application restarts
and we see that the application now crashes,
and that also get the proper error message.
So this tells me exactly how it failed and what failed.
Let's change the spec.
And now the application is running again.
You might have noticed that I really care
about error handling.
Because while everything might work well during development,
problems might arise later in production.
And I want to make sure that they exactly know
how the application will behave in such cases.
Now, with this all prepared, we can go ahead and start
with implementing session management through redis.