301 Redirect with Express
If you've created a site using Node.js, there's a great chance you've used the Express framework. Express has turned into a massive project and for good reason: it's loaded with useful functionality, an easy API, and massive community support. On of my recent projects was using Express and I loved it!
One problem I recently needed to solve was creating a 301 redirect from an old staging domain to the new production domain. Here's how you can create that 301 redirect with Express:
app.use(function forceLiveDomain(req, res, next) {
// Don't allow user to hit Heroku now that we have a domain
var host = req.get('Host');
if (host === 'serviceworker-cookbook.herokuapp.com') {
return res.redirect(301, 'https://serviceworke.rs/' + req.originalUrl);
}
return next();
});
Surprisingly the redirect code is the first argument to redirect
instead of the URL. It's important to call next()
at the end of the function or a non-match will make your app hang!
Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS. What we end up doing is repeating the same properties multiple times with each browser prefix. Yuck. Another thing we...
As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us. Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos. Another technology available...
I've been working with the Magento eCommerce solution a lot lately and I've taken a liking to a technique they use with the top bar within their administrative control panel. When the user scrolls below a specified threshold, the top bar becomes attached to the...
One thing I love about love about Safari on the iPhone is that Safari provides a darkened background effect when you click a link. It's the most subtle of details but just enforces than an action is taking place. So why not implement that...
Great code snippet and saved me a ton of time. Thanks!
You shouldn’t have the trailing slash after serviceworke.rs. I.e. instead of this:
It should be this:
Otherwise it redirects to: