caddyfile-parser
v1.1.1
Published
A simple Node.js module to parse Caddyfiles into a JSON object.
Maintainers
Readme
Caddyfile Parser
A simple Node.js module to parse Caddy server configuration files (Caddyfiles) into a structured JSON object. This parser supports directives, arguments, comments, nested blocks, and environment variables.
Features
Parses site blocks, directives, and arguments.
Correctly handles nested blocks and comma-separated domains.
Strips comments.
Substitutes environment variables (e.g., {$PORT}) from the shell environment.
Provides a helper function to easily extract all unique domains.
Installation
npm install caddyfile-parser
Usage
This module uses modern ES Module (import/export) syntax.
Parsing a Caddyfile
The parse function takes the string content of a Caddyfile and returns a structured JavaScript object.
import fs from 'fs';
import { parse } from 'caddyfile-parser';
// Read your Caddyfile into a string
const caddyfileContent = fs.readFileSync('Caddyfile', 'utf8');
// Get the structured object
const parsedConfig = parse(caddyfileContent);
console.log(parsedConfig);Getting All Domains
The getDomains helper function extracts a flat, unique array of all domains from a parsed configuration object.
import { parse, getDomains } from 'caddyfile-parser';
const caddyfileContent = `
localhost, example.com {
reverse_proxy 127.0.0.1:9000
}
sub.example.com { respond "Hello" }
`;
// First, parse the Caddyfile
const parsedConfig = parse(caddyfileContent);
// Then, extract the domains
const domains = getDomains(parsedConfig);
console.log(domains);
// Output: ['localhost', 'example.com', 'sub.example.com']License
This project is licensed under the MIT License.
