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!
My trip to Mozilla Summit 2013 was incredible. I've spent so much time focusing on my project that I had lost sight of all of the great work Mozillians were putting out. MozSummit provided the perfect reminder of how brilliant my colleagues are and how much...
Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices. While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...
Frequently asked questions can be super boring, right? They don't have to be! I've already shown you how to create fancy FAQs with MooTools -- here's how to create the same effect using jQuery.
The HTML
Simply a series of H3s and DIVs wrapper...
ContextMenu is a highly customizable, compact context menu script written with CSS, XHTML, and the MooTools JavaScript framework. ContextMenu allows you to offer stylish, functional context menus on your website.
The XHTML Menu
Use a list of menu items with one link per item. The...
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: