title: How to make a redirect (301) in Node.js / Express?
body: <p>I have a static site, a simple single page layout that I deploy in Dokku. I need to make a redirect (301) from www to non www and from <code>*.website1.com</code> to <code>website2.com</code>, but I don't know how. I was trying to use express-force-domain in npm but it makes my URL's crazy and having some redirect loops, I was trying other methods that I find on the web but none succeeds.</p>
  
  <p>my server.js is:</p>
  
  <pre><code>var express = require('express')
  var app = express();
  
  app.set('port', (process.env.PORT || 80))
  app.use(express.static(__dirname + '/public'))
  
  app.get('/', function(request, response) {
    response.send('Hello World!')
  })
  
  app.listen(app.get('port'), function() {
    console.log("Node app is running at localhost:" + app.get('port'))
  })
  </code></pre>
  
  <p>That is the version that works, but without redirects.</p>