Test Long Post
I’m a big fan of larger frameworks like Django or Rails. I know there’s a bit of a war between the folks who like the out-of-the-box kitchen sink frameworks and those who want to mash together their own framework from a series of libraries. I find that most projects (even in the microservices world) approach a size and scale quickly that makes the larger frameworks worth it. Otherwise, I often end up reinventing stuff that has been done better by people way smarter than I am.
However, I recently got a reminder that sometimes small tools built from libraries can be elegant.
I just moved this blog from blog.benjohnson.ca
to benjohnson.ca
. And in the process, I need to properly handle HTTP redirections for SEO and to generally be a good web citizen. But, I host my blog at micro.blog, and they don’t have redirection logic built in. I figured it wouldn’t take long to spin up a small redirection service that I could host at the old domain to send traffic to the new one, and that Express might be the right fit.
It took 5 minutes and 9 lines of readable code:
const { URL } = require("url");
const express = require("express");
const app = express();
app.get("/*", (req, res) => {
res.redirect(301, new URL(req.url, "https://benjohnson.ca"));
});
app.listen(process.env.PORT);
I’m sure I could solve this with a shorter Nginx config or do it with a bigger framework, but it’s hard to beat the simplicity of 9 lines of readable and understandable code that can be run with node index.js
in a Dockerfile. I even added an extra few mins to knock out tests.
JavaScript has never had a great kitchen-sink web framework (though some have tried). There’s a bazillion libraries on npm and so most people in that community are in the Libraries Are Better™ camp. I mostly give the community side-eye on this, but 5 minute project has been a good reminder that, of course, everything in programming has tradeoffs.