<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tommy Hellström on Medium]]></title>
        <description><![CDATA[Stories by Tommy Hellström on Medium]]></description>
        <link>https://medium.com/@tommyhellstrom?source=rss-338d97b10a5f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*1JC_vqRxho0AgvK2.jpg</url>
            <title>Stories by Tommy Hellström on Medium</title>
            <link>https://medium.com/@tommyhellstrom?source=rss-338d97b10a5f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 09:53:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tommyhellstrom/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Express.js/Connect.js and sessions]]></title>
            <link>https://medium.com/@tommyhellstrom/express-js-connect-js-and-sessions-4861123e5eb5?source=rss-338d97b10a5f------2</link>
            <guid isPermaLink="false">https://medium.com/p/4861123e5eb5</guid>
            <dc:creator><![CDATA[Tommy Hellström]]></dc:creator>
            <pubDate>Mon, 18 Nov 2013 14:32:26 GMT</pubDate>
            <atom:updated>2013-11-18T16:42:59.640Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to solve two problems with session management in Express/Connect</h4><p>If you use the Express framework in your Node.js application and use sessions you might encounter two problems (or at least I did…).</p><h4>Problem 1</h4><p>The session is saved on <em>every request</em> regardless if the session has been changed or not, this happens in Connect’s Session middleware (connect/lib/middleware/session.js). If you use a SessionStore other than the default MemoryStore (for example, the MongoStore) it will be (very) negative on the performance.</p><h4>Problem 2</h4><p>The Expires/Max-age on the session is updated on the server but not in the “client cookie”. For the client cookie to be updated we have to update the session.</p><h4>The solution</h4><p>Every call that requires an authenticated user will go through a method I call ensureAuthenticated. Here we can add some code that will update the session every, say, five minutes (if you use a long expiration time). When that is done a new cookie will be sent to the client.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*6AN_0uZ5kORnPOR5ML5Zpw.png" /></figure><p>One more thing is needed and that is to change Connect’s Session middleware. (Connect/lib/middleware/session.js) Original to the left, my version to the right:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LBjxZPE4XVYAg4LMWfqnpA.png" /></figure><p>I’m not comfortable with making changes in core framework files but I cannot think of another way right now.</p><h4>Conclusion</h4><p>The reason that I found out about “Problem 1&quot; above was that the mongodb.log had quite a lot of “slow query”-entries where the session collection was updated. After the changes above this is not a problem anymore. The Expires/Max-age on the session will only be updated every five minutes but that is not a problem for me as I have quite long-lived sessions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4861123e5eb5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>