node-a2conf is a JavaScript module which provides an easy way to configure apache2.
Install dependencies with npm:
npm i
Test the code with Jest framework:
npm run test
Note: This package uses husky, pinst and commitlint to automatically execute test and lint commit message before every commit.
Build production (distribution) files in the dist folder:
npm run build
It generates CommonJS (in dist/cjs folder), ES Modules (in dist/esm folder), as well as TypeScript declaration files (in dist/types folder).
const { fromFile } = require('node-a2conf');
const root = await fromFile('example.conf');
for (const vhost of root.children('VirtualHost')) {
console.log(vhost.first('ServerName'));
}
const { fromText } = require('node-a2conf');
const root = await fromText(`<VirtualHost *:80>
ServerAdmin postmaster@example.com
ServerName example.com
ServerAlias www.example.com example.example.com
ServerAlias x.example.com
DocumentRoot /usr/local/apache/htdocs/example.com
SSLEngine On
</VirtualHost>`);
root.findVHost('example.com')?.set('SSLEngine', 'Off')
console.log(root.dump());
const { Node } = require('node-a2conf');
const root = new Node();
const vhost = root.insert('<VirtualHost *:80>');
vhost.insert('ServerName example.net');
vhost.insert('DocumentRoot /var/www/examplenet/');
vhost.insert('ServerAlias www.example.net', 'servername');
console.log(root.dump());
/*
<VirtualHost *:80>
ServerName example.net
ServerAlias www.example.net
DocumentRoot /var/www/examplenet/
</VirtualHost>
*/
Returns true if this node opens section, e.g <VirtualHost>
or <IfModule>
.
Returns true if this node closes section.
Append child to node.
Append string as child to node.
Insert child after another node.
Set the value of a property given its name. If multiple properties with the same name are in the node, they will all be replaced.
Returns the open tag, e.g <VirtualHost>
.
Returns the open tag, e.g </VirtualHost>
.
Returns children matching a pattern.
Returns children matching name. Set recursive
to true to search for nested nodes as well.
Returns the first child matching a name. Set recursive
to true to search for nested nodes as well.
Extends a node with another node.
Parses raw text and replace all content from that node with the newly parsed content.
Parses a file and replace all content from that node with the newly parsed content.
Dumps the content of a node as a string into a file.
Dumps the content of a node as a string. Set depth
to the base number of spaces to append on each line.
Returns the node name as a string.
Deletes the node from its parent.
Helper function to find a VHost given its name and optional arguments. Returns a Node
or null
if no VHost matches the parameters.
Reads and parses a file then returns a Node
object.
Parses a raw text string then returns a Node
object.