Hexo is a free and open source framework to create blogs with Node.js. With a lightweight and fast engine, it allows us to quickly write new posts and deploy the blog to many web hosts, like Heroku and Github Pages.
With Hexo we can write our posts using just a simple text editor and writing the content in Markdown syntax. Then the framework generate static HTML files based on the .md files and updates all the site structure.
In this article we are going to see how to start using this framework to easily create a new blog application.
To get started with Hexo, we need to have Node.js and Git installed, because the framework is obtained from NPM and it downloads some files from a Git repository to create a new blog. Once you installed these two tools, you can check if they are really working in your computing by executing the command git --version for Git, node -v for Node.js and npm -v for NPM.
If everything is OK, you can install Hexo by runing a single command (in the Terminal/Command Prompt), that will install the Hexo Command Line Interface (CLI) from NPM:
The -g argument is used to install the package globally, so you can access it from any directory in your operational system.
When the installation finishes, you can check if the Hexo CLI is really available by runing the following command, which will show the framework version and environment informations:
From now on you can start working with the framework using the hexo command, which have many options and arguments to create and run a blog application, as we are going to see in the next session.
Creating a new blog
To create a new application with Hexo we have to run the init command, passing the name of the project in the <folder> argument. It will create a directory with this name, so we have to navigate to this folder and run npm install to restore the framework packages. In summary, we will need the following commands:
hexo init <folder>
In the end of process, the application will be ready and you can start the embedded server by running the following command and then access it from the browser using the http://localhost:4000 URL.
To stop the server, you can press CTRL+C in the Terminal/Command Prompt and then type Y to confirm.
Creating a new post
By default, Hexo support Markdown syntax to write the blog posts, and for each post it creates a new .md file in the source/posts folder. To try it, execute the following command, passing the post’s title (it it has blank spaces, surround the title with quotation marks):
Now you can open the .md file that was created in the source/posts/ folder and edit it’s content using Markdown. For example, you can copy the Listing 1’s content to this file and save it:
Listing 1. Blog post example using Markdown
title: Introduction to Hexo
date: 2016-11-26 16:17:57
This is my first blog created with Hexo.
The first lines are added by default and indicates the main post settings: the title, the date/time when it was created, and it’s tags list.
Now, run the generate command, so Hexo will create the static files that will be delivered in runtime when the user access the blog:
If you start the server again, now you you are going to see the new post int he main page, as shown in Figure 1. In the right side there is also some links to the latest posts and tags.
Figura 1. First post in the blog
Now you can explore the other Hexo commands, change the template and deploy your blog to the production environment. Then, any time you need to create a new post, just run these commands again, write your post and deploy it again.