How to get free YouTube subscribers, likes and views?
Get Free YouTube Subscribers, Views and Likes

Authentication in Node.js - #2 Connecting to MongoDB u0026 Redis

Follow
Code Realm

In this video, we will wire up our application to MongoDB and Redis. We will also configure Express sessions and set up configuration files with environment variables. Be sure to watch the previous video (especially, the latter half) so that you have the requisite project structure in place.

The default inmemory store in expresssession leaks memory and doesn't scale beyond a single process, and as such, is not suitable for production. Two popular session stores are Redis and MongoDB, Redis being the logical choice due to its builtin support for selfexpiring keys. You can use MongoDB instead, but you'd need to clean up expired sessions manually (for example, using a background job or request lottery).

If you've seen my Session Authentication in Express    • Session Authentication in Express   video, it's based on version 3 of connectredis. Version 4 no longer ships with a Redis client. You can install noderedis as before, or switch to ioredis for better maintenance and more features (including support for connection pools, for example).

In the past, you might've seen me use a single config file to export all environment variables. As the projects grows in size, so does its scope. In the long run, you'll find it more manageable to classify variables by their concern, i.e. database, cache, session, etc. This is the same pattern you will find in frameworks such as Laravel and Rails.

If you follow along til the end, you'll find that we can't boot our REST API just yet. We'll continue provisioning a dev environment with a database and a cache in the next episode. See you then.

expresssession https://www.npmjs.com/package/express...

connectredis https://www.npmjs.com/package/connect...

Project repo https://github.com/alex996/nodeauth

posted by CydayCitambumyy