concept cookie in category node

This is an excerpt from Manning's book Get Programming with Node.js.
You need the express-session module to pass messages between your application and the client. These messages persist on the user’s browser but are ultimately stored in the server. express-session allows you to store your messages in a few ways on the user’s browser. Cookies are one form of session storage, so you need the cookie-parser package to indicate that you want to use cookies and that you want your sessions to be able to parse (or decode) cookie data sent back to the server from the browser.
Use the connect-flash package to create your flash messages. This package is dependent on sessions and cookies to pass flash messages between requests. You tell your Express.js application to use cookie-parser as middleware and to use some secret passcode you choose. cookie-parser uses this code to encrypt your data in cookies sent to the browser, so choose something that’s hard to guess. Next, you have your application use sessions by telling express-session to use cookie-parser as its storage method and to expire cookies after about an hour.
You also need to provide a secret key to encrypt your session data. Specify that you don’t want to send a cookie to the user if no messages are added to the session by setting saveUninitialized to false. Also specify that you don’t want to update existing session data on the server if nothing has changed in the existing session. Last, have the application use connect-flash as middleware.
In this lesson, you learned about sessions and cookies, and saw why they’re integral parts of how data is transferred between the server and client. You also set up connect-flash to use cookies and temporarily show success and failure messages on certain views. In lesson 23, you see how to encrypt more than cookie data by implementing encryption on user passwords.

This is an excerpt from Manning's book Isomorphic Web Applications: Universal Development with React.
Node.js has no concept of a window or document. The browser doesn’t know about Node.js environment variables and has no idea what a request or response object is. Both environments know about cookies, but they handle them in different ways. In chapter 10, you’ll look at strategies for dealing with these environment tensions.
Rather than doing a deep dive into any specific topic, this chapter covers a range of topics that will make your app perform better and improve your end-user experience. This includes React performance, Node.js performance, and various caching strategies. The last section of this chapter covers handling cookies in an isomorphic application and the trade-offs that this creates with some of the caching strategies.
Modern web applications use cookies in the browser almost without exception. Even if your main product isn’t directly using cookies, any ads, tracking, or other third-party tools that you use on your site will take advantage of cookies. Cookies let the web app know that the same person has come back over time. Figure 11.5 illustrates how this works.
Figure 11.5. Repeat visits by the same user on the server. Saving cookies lets you store information about the user that can be retrieved during future sessions.
![]()
Listing 11.9 shows an example module that handles both the browser and server cookie parsing for you. It uses Universal Cookie to help manage the cookies in both environments: www.npmjs.com/package/universal-cookie. You need to install this library for the code to work:
By fetching cookies with an action, you can standardize the way the app interacts with cookies. The following listing shows how to add a storeUserId action to fetch and store the user ID. Add this code to the app-action-creators file.
Listing 11.10. Accessing cookies on the server—src/shared/app-action-creators.es6
import UAParser from 'ua-parser-js'; import cookies from './cookies.es6'; #1 export const PARSE_USER_AGENT = 'PARSE_USER_AGENT'; export const STORE_USER_ID = 'STORE_USER_ID'; #2 export function parseUserAgent(requestHeaders) {} export function storeUserId(requestHeaders) { #3 const userId = cookies.get('userId', requestHeaders); #4 return { userId, #5 type: STORE_USER_ID #2 }; } export default { parseUserAgent, storeUserId }; #1 Import the cookie module. #2 Add a type for the new action. #3 Add the action, which takes in requestHeaders so that it works on the server. #4 Pass the cookie name and requestHeaders to the cookie module. #5 Put the userId value on the action. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":62}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":62}],[{\"line\":8,\"ch\":0},{\"line\":8,\"ch\":62}],[{\"line\":9,\"ch\":2},{\"line\":9,\"ch\":62}],[{\"line\":10,\"ch\":2},{\"line\":10,\"ch\":10}],[{\"line\":11,\"ch\":4},{\"line\":11,\"ch\":62}],[{\"line\":12,\"ch\":4},{\"line\":12,\"ch\":62}],[{\"line\":13,\"ch\":2},{\"line\":13,\"ch\":4}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":2}],[{\"line\":6,\"ch\":48},{\"line\":6,\"ch\":49}],[{\"line\":13,\"ch\":2},{\"line\":13,\"ch\":3}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":1}],[{\"line\":19,\"ch\":0},{\"line\":19,\"ch\":1}],[{\"line\":9,\"ch\":37},{\"line\":9,\"ch\":38}],[{\"line\":11,\"ch\":10},{\"line\":11,\"ch\":11}],[{\"line\":17,\"ch\":16},{\"line\":17,\"ch\":17}],[{\"line\":23,\"ch\":17},{\"line\":23,\"ch\":18}],[{\"line\":8,\"ch\":16},{\"line\":8,\"ch\":27}],[{\"line\":18,\"ch\":2},{\"line\":18,\"ch\":13}]]"} !@%STYLE%@!