Create a Blog in a Subdirectory with Hexo

Create a Blog in a Subdirectory with Hexo

When getting started with content marketing for your product the first step is usually to create a blog. But how you do that has countless options. Do you host your own, use a third-party service, and what URL should it be at?

Usually, the decision on URL boils down to choosing between a blog on a subdomain like blog.yourproduct.com or on a subdirectory like yourproduct.com/blog. Google says that it treats both options the same but there is a lot of disagreement in the SEO space.

In data shared by multiple sites who switched between subdomains and subdirectories, there was a clear trend in organic traffic from Google. Subdirectories performed significantly better. Up to 40% in some cases.

However many still choose to host their blogs ona  subdomain and this could be down to the difficulty of hosting a blog in a subdirectory of your product's site. If you're using a dynamic blog CMS like Ghost or Wordpress it's just not worth the effort.

The simplest option is to use a static blog engine that can be easily routed to on that subdirectory. For my project, InboxReads, I knew any percentage difference in organic traffic would be important to capture so a subdirectory blog was a must.

Depending on your main project's stack the steps and tools will vary. Here I'll share how I got a blog running for a Node.js project.

Blog Engine

There are a lot of static site generators available for Node but I chose Hexo a static blog framework. It's easy to get started, has a ton of themes, and fits all the criteria to be easily hosted in a subdirectory.

First, you'll want to install the hexo-cli with npm install hexo-cli -g then you can create your blog with hexo init blog. This creates a blog folder in your project with all the contents for your blog. In that folder, you can install dependencies with npm install then hexo server to check out your brand new blog.

Writing articles with Hexo is as simple as using the hexo new command. It creates a markdown file you can type your article in.

The next step is to generate the static files for your blog. Which just requires the hexo generate command and it'll output the static files for your blog in the public directory.

Routing

In your Node.js project you'll want to create a route to the public directory that was generated above.

In your app.js add a new static route like:

app.use('/blog', express.static(path.join(__dirname, '/blog/public')));

This create a route at /blog in your Node app that serves the static blog you generated above. And that's it. Now you can just need deploy and your blog is available.