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

Опубликовано: 20 Декабрь 2019
на канале: Code Realm
25,516
347

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 in-memory store in express-session 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 built-in support for self-expiring 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 connect-redis. Version 4 no longer ships with a Redis client. You can install node-redis 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.

express-session https://www.npmjs.com/package/express...

connect-redis https://www.npmjs.com/package/connect...

Project repo https://github.com/alex996/node-auth


Смотрите видео Authentication in Node.js - #2 Connecting to MongoDB & Redis онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Code Realm 20 Декабрь 2019, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 25,51 раз и оно понравилось 34 людям.