Replace hostname in Node.js + Express.js to decode punycode domain
<p>I'm developing a web-app on a cyrillic domain. Currently, this domain hosts a "parked page", saying the site is under construction. If I access it in Chrome, I see punycode in the address bar. Safari decodes it, though. For the development purposes, I have modified my <code>/etc/hosts</code> file to be able to access localhost via a test cyrillic domain. However, both Chrome and Safari fail to decode the hostname.</p>

<p>I have looked up this issue, but could not find any sensible solution. There is a module for Node.js called <code>punycode</code>. Now, if my <code>req.url</code> contains cyrillic characters, it gets <code>URIComponent</code>-encoded, hence I've written a middleware to decode it:</p>

<pre><code>app.use(function(req, res, next) {
    var url = req.url,
        decoded = decodeURIComponent(url);

    if (url !== decoded) req.url = decoded;
    next();
});
</code></pre>

<p>It works fine, I can use cyrillic routing now. But when I try to apply this logic to hostname, it doesn't work:</p>

<pre><code>app.use(function(req, res, next) {
    var hostname = req.hostname,
        decoded = punycode.toUnicode(hostname);

    if (hostname !== decoded) req.hostname = decoded;
    // I have also tried return res.redirect('https://' + decoded + ':' + ...);
    next();
});
</code></pre>

<p>Any help is very much appreciated. Thanks!</p>