Hexo Development Server

3.0.0 · active · verified Sun Apr 19

hexo-server is the official server module for the static site generator Hexo. It provides a local development server to preview Hexo sites during development. Currently at stable version 3.0.0, it follows Hexo's release cadence, often aligning major version bumps with Node.js End-of-Life cycles. Key differentiators include its tight integration with the Hexo ecosystem, automatic recompilation of site assets on changes, and configurable options for port, IP, logging, and static file serving. It uses a Connect/Express-like middleware stack to serve generated files and handle live reloading, making it an essential tool for Hexo developers.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to start the Hexo development server for a given project directory using the Hexo CLI, including basic project setup.

const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');

// Ensure Hexo is installed globally or locally
// For a quickstart, we'll assume a hexo project exists

const hexoProjectPath = path.join(__dirname, 'my-hexo-blog');

// Create a dummy Hexo project for demonstration if it doesn't exist
if (!fs.existsSync(hexoProjectPath)) {
  console.log('Creating a dummy Hexo blog for demonstration...');
  // This would typically be `hexo init my-hexo-blog`
  fs.mkdirSync(hexoProjectPath);
  fs.writeFileSync(path.join(hexoProjectPath, '_config.yml'), 'title: My Hexo Blog\nport: 4000\n');
  fs.mkdirSync(path.join(hexoProjectPath, 'source'));
  fs.writeFileSync(path.join(hexoProjectPath, 'source', 'index.md'), '---\ntitle: Hello World\n---');
  console.log('Dummy Hexo blog created.');
}

// Start the Hexo server from within the project directory
console.log(`Starting Hexo server for project: ${hexoProjectPath}`);
const hexoServerProcess = spawn('hexo', ['server', '-p', '4001', '-i', '127.0.0.1'], {
  cwd: hexoProjectPath,
  stdio: 'inherit',
  shell: true // Required on Windows for 'hexo' command to be found
});

hexoServerProcess.on('error', (err) => {
  console.error('Failed to start Hexo server:', err);
});

hexoServerProcess.on('exit', (code) => {
  console.log(`Hexo server exited with code ${code}`);
});

// Keep the process alive for a few seconds to demonstrate, then exit
setTimeout(() => {
  console.log('Stopping Hexo server after 10 seconds...');
  hexoServerProcess.kill(); // Terminate the child process
}, 10000);

view raw JSON →