Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
For many web applications, establishing a session is a crucial way of identifying a user. Sessions make it possible for people to log into a website and not have to re-enter their password on every page. In this video we will add sessions for our application and use them to track a simple piece of data. First let's go to the exercise files. Go to chapter seven, video four. And then copy the start folder to the desktop. Now go to terminal and change directory to that start folder. I'm just going to drag it in to get the path.
And then finally type NPM install, and then press Return. Now in certain environments and frameworks, session storage is handled for you. Consequently, you can end up taking them for granted. When I first started writing express applications, I quickly discovered sessions are now my responsibility. Not only did I have to turn them on, I also had to figure out a way of storing them. While you can just let express store the session information in memory, there are some down sides this approach. First if you are running your application on multiple processes, the session will only be available in the process where it was started.
If you have a load balancer in front of your web application and the user gets sent to another process, their session will suddenly disappear. Another disadvantage is that if your server has to be restarted, all of the sessions are lost and have to be reestablished. A final disadvantage is that your sessions storage space is limited by the amount of memory you have set aside for your application. Now this may not be such a big deal if you manage your own server and you give your application loads of memory. But it may become an issue if you're running the application on a cloud surface where you're paying for memory incrementally.
An overall better approach is to store your sessions in a central location, such as a MongoDB Database. Fortunately there's Middleware available that will tie your Express sessions to a MongoDB Database. It's called Connect Mongo. Express is written on top of a smaller framework called Connect, so this Middleware is also compatible with Connect. Let's install it now. I'm going to type npm install and then --save, and then connect- mongo. If you see a warning on this line, disregard it.
Now that we've installed connect-mongo, let's add it to the application. I'm going to open up the start folder in sublime text, and then I'm going to go to app.js. Just like any other module, we're going to require connect mongo. We're going to store it in a variable called Mongostore. Now before ending this line we're going to pass express into connect Mongo. Next, let's set up express to use sessions. Go to the line just before express.body parser, and make some space.
We're going to add another app.use call here. The first thing we're going to add is the cookie parser. This line is crucial because this is the software that's going to read the cookies that the browser's sending to the server. Now let's set up the sessions. Again, we're using apt.use. Now we're going to call express.session. This function takes an object as an argument. And this object is going to configure the sessions. First, we're going to type in a secret. Typically, you want to add a randomized string here.
The secret is going to be used to encrypt the session information. You can just leave the string keyboard cat here or you can have your cat actually walk on the keyboard to generate a randomized string. And then the other property we're going to define is the store property. This is going to tell express where we want to store our sessions. So on line eight, we defined Mongostore, and that's where we're going to store the sessions. So let's declare a new Mongostore instance. That constructor takes an object as well.
We need to set one property here for mongoose_connection. Now this file doesn't currently have a database connection available. But we can pass it in. So for the moment let's type in db. Now let's go up to line six and add db as another argument. Save this file, and now let's go to server.js. We're requiring the db on line three and now we can pass it in on line four. So now, sessions are available in our Express application and they're going to be stored in the MongoDB database.
However, we need to actually do something with the sessions. Let's track the last flight that someone viewed and display it on the list of all the arrivals. To do that, let's go to the routes. Go to index.js, and then go to the flight function. And then using the data that we already have here, we can set the session information. On the request object there is a new property called session. This session property is an object, and we can set the properties of this session object to whatever we want.
So in this case, I'm going to set lastNumber, and then I'm going to set that property to number. Now let's go to the arrivals handler. We're currently just passing the arrivals into the Jade view. However, we could also pass in the last viewed flight. Let's do that by adding it here. And again, we're going to get that data out of the session. The session is always stored in the request. Now let's go to our Jade view and adjust it. I'm going to add a paragraph tag here and I'm going to use the last number variable.
But let's concatenate this string here. So now whenever we go the arrivals page, it'll show us which flight was the last one viewed and it's going to show it to us in a paragraph tag. Now let's start up the server and test all this. Go to terminal and type node space server and then press return. It looks like we have an extra semicolon in app.js. Let's go there now and fix this. Since we're calling express.session, within an argument to another function, we don't need a semicolon.
Remove it and then save the file, and let's try this again. Press the up arrow on your keyboard, and then, press return. So now the server is listening on port 3000. Let's load up one of the flights. Go to local host at port 3000 and let's go to flight 33. Now let's go to the arrivals page. I'm going to open it up in a separate tab. And it's now showing that the last flight we viewed was 33. Let's go to flight 18. Another thing to note, is that this is specific session information.
Other people going to this website are not going to see this information. Let's open up a session free window. Go to file and new incognito window, and then paste in the arrivals URL. You'll notice here that it says the last flight viewed is undefined. That's because the session is in this browser window, and not in the session free window. The connect Mongo middleware makes it easy to store all of your express sessions in MongoDB. Once you've enabled sessions, variables can be set as a request object and persisted from request to request for that user.
In the next video, we'll add a login form and use sessions to keep users logged in.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97140 Viewers
61 Video lessons · 84431 Viewers
71 Video lessons · 68637 Viewers
56 Video lessons · 101128 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.