title: On Heroku web apps, how to redirect from users from .herokuapp.com to custom domain?
body: <p>I am using NodeJS, specifically, I am using the tutorial code from node-js-getting-started. So I have a custom domain, but the default heroku domain is also active, and I would like to have the custom domain be the only one in use. Heroku said to do this.</p>
  
  <blockquote>
    <p>Your app’s Heroku domain always remains active, even if you set up a custom domain. If you want users to use the custom domain exclusively, your app should send HTTP status 301 Moved Permanently to tell web browsers to use the custom domain. The Host HTTP request header field will show which domain the user is trying to access; send a redirect if that field is example.herokuapp.com.</p>
  </blockquote>
  
  <p>I don't really understand how to do this. I'm guessing I have to change something in the index.js file.</p>
  
  <pre><code>const express = require('express')
  const path = require('path')
  
  const PORT = process.env.PORT || 5000
  
  express()
    .use(express.static(path.join(__dirname, 'public')))
    .set('views', path.join(__dirname, 'views'))
    .set('view engine', 'ejs')
    .get('/', (req, res) =&gt; res.render('pages/index'))
    .listen(PORT, () =&gt; console.log(`Listening on ${ PORT }`))
  </code></pre>