-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Mastering Node.js Web Development
By :
Cookies are suited to storing small amounts of data, but that data has to be sent to the application with every request, and any changes to that data have to be signed and sent in the response.
An alternative is to have the application store the data and include just a reference to that data in the cookie. This allows larger amounts of data to be stored without that data being included in every request and response.
Session data can be stored as a set of key/value pairs, which makes it easy to use JavaScript objects to represent data. I am going to start by creating a memory-based session system and then introduce persistent storage with a database, using a repository layer to make the transition easier. Create the src/server/sessions folder and add to it a file named repository.ts with the content shown in Listing 13.11.
Listing 13.11: The contents of the repository.ts file in the src/server/sessions folder
export type Session = {
id: string,...