<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have searched the <a href="https://github.com/zeit/next.js/issues">issues</a> of this repository and believe that this is not a duplicate.</li>
</ul>
<h2 dir="auto">Expected Behavior</h2>
<p dir="auto">In page component lifecycle method, it could access <code class="notranslate">req</code> and <code class="notranslate">res</code> in server side.</p>
<h2 dir="auto">Current Behavior</h2>
<p dir="auto">In static method <code class="notranslate">getInitialProps</code>, the argument <code class="notranslate">context</code> has fields <code class="notranslate">req</code> and <code class="notranslate">res</code> which is express request &amp; response. However, there is no way to access <code class="notranslate">req</code> and <code class="notranslate">res</code> in page component lifecycle methods, because life cycle methods are instance methods.</p>
<h2 dir="auto">Context</h2>
<p dir="auto">In some cases, it is preferred to have access to <code class="notranslate">req</code> and <code class="notranslate">res</code> in component lifecycle methods in server side. I tried to have <code class="notranslate">getInitialProps</code> to return <code class="notranslate">req</code> &amp; <code class="notranslate">res</code> as props, but <code class="notranslate">next.js</code> throw warnings about that these objects are not OK to be dehydrated.</p>
<p dir="auto">For the time being, I used <a href="https://www.npmjs.com/package/request-local-storage" rel="nofollow">request-local-storage</a> to transfer access from <code class="notranslate">getIntialProps</code> method. I hope <code class="notranslate">next.js</code> could have some native support for this scenario.</p>
<p dir="auto">Can you please add an example here:</p>
<p dir="auto"><a href="https://github.com/zeit/next.js/tree/master/examples">https://github.com/zeit/next.js/tree/master/examples</a></p>
<p dir="auto">that uses API routes to handle POST requests with also a custom Express server to serve the application? I feel like there are conflicts between routes provided by the Express server, the file system routing of Next and the API routes within the <code class="notranslate">api</code> folder. The following are related issues:</p>
<p dir="auto"><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="467850261" data-permission-text="Title is private" data-url="https://github.com/vercel/next.js/issues/7960" data-hovercard-type="issue" data-hovercard-url="/vercel/next.js/issues/7960/hovercard" href="https://github.com/vercel/next.js/issues/7960">#7960</a></p>
<p dir="auto"><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="474663186" data-permission-text="Title is private" data-url="https://github.com/vercel/next.js/issues/8178" data-hovercard-type="issue" data-hovercard-url="/vercel/next.js/issues/8178/hovercard" href="https://github.com/vercel/next.js/issues/8178">#8178</a></p>
<p dir="auto"><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="473174070" data-permission-text="Title is private" data-url="https://github.com/vercel/next.js/issues/8120" data-hovercard-type="issue" data-hovercard-url="/vercel/next.js/issues/8120/hovercard" href="https://github.com/vercel/next.js/issues/8120">#8120</a></p>
<p dir="auto">In order to make it work now I have to do something that seems pretty crazy:</p>
<p dir="auto"><code class="notranslate">pages/api/signin.js</code></p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="'use strict';
module.exports = require('../../src/apiHandlers').postSignIn;"><pre class="notranslate"><span class="pl-s">'use strict'</span><span class="pl-kos">;</span>
<span class="pl-smi">module</span><span class="pl-kos">.</span><span class="pl-c1">exports</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'../../src/apiHandlers'</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-c1">postSignIn</span><span class="pl-kos">;</span></pre></div>
<p dir="auto"><code class="notranslate">server.js</code></p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="expressApp.post('/api/signin', require('./pages/api/signin'))"><pre class="notranslate"><span class="pl-s1">expressApp</span><span class="pl-kos">.</span><span class="pl-en">post</span><span class="pl-kos">(</span><span class="pl-s">'/api/signin'</span><span class="pl-kos">,</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'./pages/api/signin'</span><span class="pl-kos">)</span><span class="pl-kos">)</span></pre></div>