---
title: 'ncms – Node-RED flat-file CMS'
url: 'https://niklas-stephan.de/en/wiki/self-hosted/ncms'
markdown: 'https://niklas-stephan.de/en/wiki/self-hosted/ncms.md'
lang: en
date: '2022-04-15'
description: 'ncms – Node-RED flat-file CMS Verified: April 2022 (flow version 0.60), ran niklas-stephan.de until the 2024 relaunch. Sources: https://github.com/handtrixx/ncms; this page documents its architecture and flow nodes. ncms ("niklas stephan''s / node-red / not another Content Management System") is a f…'
---

#  ncms – Node-RED flat-file CMS 

*Verified: April 2022 (flow version 0.60), ran niklas-stephan.de until the 2024 relaunch. Sources: <https://github.com/handtrixx/ncms>; this page documents its architecture and flow nodes.*

ncms ("niklas stephan's / node-red / not another Content Management System") is a flat-file, headless WCMS: no database, everything is files. The backend is Node-RED with Node.js functions written into its nodes; content is markdown; a deployment renders the whole site to static files that a web server serves directly.

## Properties

- Deployment (static file generation) duration: 70–320 ms.
- Frontend: start page ~670 KB incl. animations/images, fully loaded client-side in 100–300 ms.
- Multi-language pages and posts; comment function; media management via file upload; template + snippet HTML skeleton; no frontend frameworks (vanilla JS); automatic social/SEO meta generation; full-text search over a generated local JSON index; "published" attribute for releases.
- Code changes are versioned via Git/GitHub; content changes trigger a deployment manually or via webhook.

![Deployment duration in the Node-RED log](https://niklas-stephan.de/user/pages/wiki/02.self-hosted/ncms/03-ncms-deployment.webp)

## Container setup

The whole build runs in one Docker Compose stack based on the official Node-RED image (<https://hub.docker.com/r/nodered/node-red>):

```yaml
<span class="hljs-attr">services:</span>
  <span class="hljs-attr">ncms:</span>
    <span class="hljs-attr">container_name:</span> <span class="hljs-string">ncms</span>
    <span class="hljs-attr">hostname:</span> <span class="hljs-string">ncms</span>
    <span class="hljs-attr">image:</span> <span class="hljs-string">nodered/node-red:latest</span>
    <span class="hljs-attr">restart:</span> <span class="hljs-string">always</span>
    <span class="hljs-attr">environment:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">TZ=Europe/Berlin</span>
    <span class="hljs-attr">networks:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">dmz</span>
    <span class="hljs-attr">logging:</span>
      <span class="hljs-attr">options:</span>
        <span class="hljs-attr">max-size:</span> <span class="hljs-string">"10m"</span>
        <span class="hljs-attr">max-file:</span> <span class="hljs-string">"3"</span>
    <span class="hljs-attr">volumes:</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">./data:/data</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">./dist:/dist:rw</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">./src:/src:rw</span>
      <span class="hljs-bullet">-</span> <span class="hljs-string">/etc/localtime:/etc/localtime:ro</span>

<span class="hljs-attr">networks:</span>
  <span class="hljs-attr">dmz:</span>
    <span class="hljs-attr">external:</span> <span class="hljs-literal">true</span>
```

Serving: the reverse proxy is Nginx Proxy Manager (<https://nginxproxymanager.com/>); its web root is symlinked (`ln -s`) into `./dist`, so no extra web server or double deployment is needed. The Node-RED backend sits behind its own subdomain.

## Directory structure

Under `./src`:

```text
src/
  assets/   css, fonts, img, js, json (translation keys)
  md/posts/ all posts in markdown (.md, front matter as JSON)
  media/    images (max one subfolder level); converted to webp + thumb on deploy
  snippets/ HTML elements repeated on every page
  templates frontend source files, enriched during deployment
```

- **assets** – files reused across the frontend (Bootstrap 5 CSS/JS, own styles and functions); `json` holds static translation keys.
- **md/posts** – all posts as markdown.
- **media** – all images for posts; the deployment converts them to `.webp` and generates a 440 px thumbnail per image.
- **snippets** – repeated HTML elements.
- **templates** – frontend sources enriched with content at deployment.

### Templates

| File | Purpose |
|---|---|
| `404.html` | error page for invalid queries |
| `blog.html` | overview of all posts |
| `index.html` | start page |
| `post.html` | per-post rendering template |
| `privacy-policy.html` | privacy policy and imprint |
| `robots.txt` | crawler instructions |
| `search.html` | search page; results come from the JSON index without a server query |

### Snippets

`footer.html`, `head.html`, `navbar.html`, `script.html` – injected into every template at deployment, so a change (e.g. in the navigation) is made exactly once.

## Node-RED flow (v0.60)

Node-RED's plain flow concept is used – no palette plugins; Node.js npm modules are loaded inside the function nodes. The full flow is in the GitHub repository.

![The ncms Node-RED flow](https://niklas-stephan.de/user/pages/wiki/02.self-hosted/ncms/05-ncms-node-red-flow.webp)

### `/deploy` (http in)

Starts a deployment via webhook, e.g.:

```bash
curl -X POST -d <span class="hljs-string">'key=----'</span> https://ncms.example.com/deploy
```

### `catch key` (function)

Webhook authentication: the transferred key is compared against `/data/deploy.key`. npm modules bound under "Setup": `fs-extra` as `fse`.

```javascript
<span class="hljs-keyword">const</span> transferedKey = msg.payload.key;
<span class="hljs-keyword">const</span> systemKey = fse.readFileSync(<span class="hljs-string">'/data/deploy.key'</span>, <span class="hljs-string">'utf8'</span>)

<span class="hljs-keyword">if</span> (transferedKey == systemKey) {
    msg.payload = <span class="hljs-string">"Deployment Started"</span>;
    msg.statusCode = <span class="hljs-number">200</span>;
    msg.type = <span class="hljs-string">"webhook"</span>;
    msg.starttime = <span class="hljs-built_in">Date</span>.now();
    <span class="hljs-keyword">return</span> [<span class="hljs-literal">null</span>,msg];
} <span class="hljs-keyword">else</span> {
    msg.payload = <span class="hljs-string">"Wrong authentication!"</span>
    msg.statusCode = <span class="hljs-number">400</span>;
    <span class="hljs-keyword">return</span> [msg,<span class="hljs-literal">null</span>];  
}
```

Two outputs: key matches → output 2 continues; otherwise the error goes to output 1 and the run aborts.

### `deploy` (inject)

Same deployment start, but manually from the Node-RED UI.

### `get posts` (function)

Reads all `*.md` posts, renders markdown to HTML and collects posts as objects in `msg.posts`. Modules: `fs-extra` as `fse`, `markdown-it` plus the `markdown-it-front-matter`, `markdown-it-linkify-images` and `markdown-it-link-attributes` plugins.

```javascript
msg.baseurl = <span class="hljs-string">"https://niklas-stephan.de"</span>
msg.dist = {};
msg.posts = [];
<span class="hljs-keyword">const</span> path = <span class="hljs-string">'/src/md/posts/'</span>;
<span class="hljs-keyword">const</span> postfiles = fse.readdirSync(path)
<span class="hljs-keyword">const</span> alength = postfiles.length;

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i=<span class="hljs-number">0</span>; i<alength; i++) {
    <span class="hljs-keyword">var</span> srcFile = path+postfiles[i];
    <span class="hljs-keyword">var</span> distFilename = postfiles[i].split(<span class="hljs-string">'.'</span>)[<span class="hljs-number">0</span>]+<span class="hljs-string">".html"</span>;
    <span class="hljs-keyword">var</span> srcContent = fse.readFileSync(srcFile, <span class="hljs-string">'utf8'</span>)

    <span class="hljs-keyword">var</span> md = <span class="hljs-keyword">new</span> markdownIt({
        <span class="hljs-attr">html</span>: <span class="hljs-literal">true</span>,<span class="hljs-attr">linkify</span>: <span class="hljs-literal">true</span>,<span class="hljs-attr">typographer</span>: <span class="hljs-literal">true</span>,<span class="hljs-attr">breaks</span>: <span class="hljs-literal">true</span>})
        .use(markdownItFrontMatter, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">metainfo</span>) </span>{meta = <span class="hljs-built_in">JSON</span>.parse(metainfo);})
        .use(markdownItLinkifyImages, {<span class="hljs-attr">target</span>: <span class="hljs-string">'_blank'</span>,<span class="hljs-attr">linkClass</span>: <span class="hljs-string">'custom-link-class'</span>,<span class="hljs-attr">imgClass</span>: <span class="hljs-string">'custom-img-class'</span>})
        .use(markdownItLinkAttributes, { <span class="hljs-attr">attrs</span>: {<span class="hljs-attr">target</span>: <span class="hljs-string">"_blank"</span>,<span class="hljs-attr">rel</span>: <span class="hljs-string">"noopener"</span>,}
    });

    distContent = md.render(srcContent);

    <span class="hljs-keyword">let</span> data = {<span class="hljs-string">"srcFile"</span>:<span class="hljs-string">""</span>+srcFile+<span class="hljs-string">""</span>,<span class="hljs-string">"srcContent"</span>:<span class="hljs-string">""</span>+srcContent+<span class="hljs-string">""</span>,<span class="hljs-string">"distContent"</span>:<span class="hljs-string">""</span>+distContent+<span class="hljs-string">""</span>,<span class="hljs-string">"distFilename"</span>:<span class="hljs-string">""</span>+distFilename+<span class="hljs-string">""</span>,...meta};

    msg.posts.push(data)
}

<span class="hljs-keyword">return</span> msg;
```

### `get snippets` (function)

```javascript
msg.snippets = {};
<span class="hljs-keyword">const</span> path = <span class="hljs-string">'/src/snippets/'</span>;
<span class="hljs-keyword">const</span> files = fse.readdirSync(path)
<span class="hljs-keyword">const</span> alength = files.length;

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i=<span class="hljs-number">0</span>; i<alength; i++) {
    <span class="hljs-keyword">var</span> srcFile = path+files[i];
    <span class="hljs-keyword">var</span> srcContent = fse.readFileSync(srcFile, <span class="hljs-string">'utf8'</span>)

    msg.snippets[files[i]] = srcContent;   
}

<span class="hljs-keyword">return</span> msg;
```

### `get templates` (function)

```javascript
msg.templates = {};
<span class="hljs-keyword">const</span> path = <span class="hljs-string">'/src/templates/'</span>;
<span class="hljs-keyword">const</span> files = fse.readdirSync(path)
<span class="hljs-keyword">const</span> alength = files.length;

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i=<span class="hljs-number">0</span>; i<alength; i++) {
    <span class="hljs-keyword">var</span> srcFile = path+files[i];
    <span class="hljs-keyword">var</span> srcContent = fse.readFileSync(srcFile, <span class="hljs-string">'utf8'</span>)

    msg.templates[files[i]] = srcContent;  
}

<span class="hljs-keyword">return</span> msg;
```

### `create index.html` (function)

Meta tags for social/SEO in the first block; snippet, title and metadata injection into template placeholders in the second. Result: `msg.dist.index`.

```javascript
msg.dist.index = <span class="hljs-string">""</span>;
<span class="hljs-keyword">var</span> ogmetalang = <span class="hljs-string">"de_DE"</span>;
<span class="hljs-keyword">var</span> ogmeta = <span class="hljs-string">`
<meta property="og:type" content="website">
<meta property="og:locale" content="`</span>+ogmetalang+<span class="hljs-string">`">
<meta property="og:site_name" content="niklas-stephan.de">
<link rel="canonical" href="`</span>+msg.baseurl+<span class="hljs-string">`/index.html">
<meta name="description" content="Projekte und Posts aus der Welt von IoT, Musik und mehr">
<meta property="og:title" content="Projects & Blog - niklas-stephan.de">
<meta property="og:description" content="Projekte und Posts aus der Welt von IoT, Musik und mehr">
<meta property="og:url" content="`</span>+msg.baseurl+<span class="hljs-string">`/index.html">
<meta property="og:image" content="`</span>+msg.baseurl+<span class="hljs-string">`/assets/img/me_logo.webp">
<meta property="og:image:secure_url" content="`</span>+msg.baseurl+<span class="hljs-string">`/assets/img/me_logo.webp">
<meta name="twitter:card" content="summary">
<meta name="twitter:description" content="Projekte und Posts aus der Welt von IoT, Musik und mehr">
<meta name="twitter:title" content="Projects & Blog - niklas-stephan.de">
<meta name="twitter:image" content="`</span>+msg.baseurl+<span class="hljs-string">`/assets/img/me_logo.webp">`</span>

msg.dist.index = msg.templates[<span class="hljs-string">"index.html"</span>].replace(<span class="hljs-string">"<!-- html head from head.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"head.html"</span>]);
msg.dist.index = msg.dist.index.replace(<span class="hljs-string">"<!-- Top Navigation from navbar.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"navbar.html"</span>]);
msg.dist.index = msg.dist.index.replace(<span class="hljs-string">"<!-- footer navigation from footer.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"footer.html"</span>]);
msg.dist.index = msg.dist.index.replace(<span class="hljs-string">"<!-- Javascript from script.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"script.html"</span>]);
msg.dist.index = msg.dist.index.replace(<span class="hljs-string">"<!-- PAGE TITLE -->"</span>,<span class="hljs-string">"Home"</span>);
msg.dist.index = msg.dist.index.replace(<span class="hljs-string">"<!-- meta tags -->"</span>,ogmeta);

<span class="hljs-keyword">return</span> msg;
```

### `create 404.html`, `create privacy-policy.html`, `create search.html`

The same snippet/title/meta pattern: snippets inserted into the respective template, page title set, results as `msg.dist.errorpage`, `msg.dist.privacy`, `msg.dist.search`.

```javascript
msg.dist.privacy = <span class="hljs-string">""</span>;

msg.dist.privacy = msg.templates[<span class="hljs-string">"privacy-policy.html"</span>].replace(<span class="hljs-string">"<!-- html head from head.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"head.html"</span>]);
msg.dist.privacy = msg.dist.privacy.replace(<span class="hljs-string">"<!-- Top Navigation from navbar.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"navbar.html"</span>]);
msg.dist.privacy = msg.dist.privacy.replace(<span class="hljs-string">"<!-- footer navigation from footer.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"footer.html"</span>]);
msg.dist.privacy = msg.dist.privacy.replace(<span class="hljs-string">"<!-- Javascript from script.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"script.html"</span>]);
msg.dist.privacy = msg.dist.privacy.replace(<span class="hljs-string">"<!-- PAGE TITLE -->"</span>,<span class="hljs-string">"Datenschutz & Impressum"</span>);

<span class="hljs-keyword">return</span> msg;
```

### `create search index.json` (function)

One JSON entry per post so search runs client-side against the generated index; result `msg.dist.searchindex`.

```javascript
<span class="hljs-keyword">var</span> alength = msg.posts.length;
<span class="hljs-keyword">var</span> index = <span class="hljs-string">"["</span>;

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i=<span class="hljs-number">0</span>; i<alength; i++) {
    index = index+<span class="hljs-string">`{"lang":"`</span>+msg.posts[i].language+<span class="hljs-string">`","link":"/posts/`</span>+msg.posts[i].distFilename+<span class="hljs-string">`","headline":"`</span>+msg.posts[i].title+<span class="hljs-string">`","content":"`</span>+msg.posts[i].distContent.replace(<span class="hljs-regexp">/[^a-zA-Z0-9]/g</span>, <span class="hljs-string">' '</span>)+<span class="hljs-string">`"},`</span>;
}
index = index.slice(<span class="hljs-number">0</span>, <span class="hljs-number">-1</span>);
index = index+<span class="hljs-string">"]"</span>;

msg.dist.searchindex = index;

<span class="hljs-keyword">return</span> msg;
```

### `create sitemap.xml` (function)

```javascript
<span class="hljs-keyword">var</span> xml = <span class="hljs-string">`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <url>
        <loc>`</span>+msg.baseurl+<span class="hljs-string">`/</loc>
        <priority>1.00</priority>
    </url>
    <url>
        <loc>`</span>+msg.baseurl+<span class="hljs-string">`/index.html</loc>
        <priority>0.80</priority>
    </url>
    <url>
        <loc>`</span>+msg.baseurl+<span class="hljs-string">`/blog.html</loc>
        <priority>0.80</priority>
    </url>`</span>

msg.posts.forEach(postxml);

xml = xml + <span class="hljs-string">`
</urlset>`</span>
msg.dist.sitemap = xml;
<span class="hljs-keyword">return</span> msg;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">postxml</span>(<span class="hljs-params">item</span>) </span>{
     <span class="hljs-keyword">if</span> (item.published == <span class="hljs-literal">true</span> ) {
        xml = xml + <span class="hljs-string">`
        <url>
            <loc>`</span>+msg.baseurl+<span class="hljs-string">`/posts/`</span>+item.distFilename+<span class="hljs-string">`.html</loc>
            <priority>0.64</priority>
        </url>`</span>
     }
}
```

### `create posts` (function)

Per-post page generation: language-dependent OG locale, meta block from front-matter fields (`title`, `excerpt`, `imgurl`), template placeholder replacement; result `msg.dist.posts` keyed by filename.

```javascript
msg.dist.posts = {};
<span class="hljs-keyword">var</span> alength = msg.posts.length;
<span class="hljs-keyword">var</span> data = <span class="hljs-string">""</span>;
<span class="hljs-keyword">var</span> ogmetalang = <span class="hljs-string">""</span>;
<span class="hljs-keyword">var</span> ogmeta = <span class="hljs-string">""</span>;
<span class="hljs-keyword">var</span> postdate = <span class="hljs-string">""</span>;

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i=<span class="hljs-number">0</span>; i<alength; i++) {

    <span class="hljs-keyword">if</span> (msg.posts[i].language == <span class="hljs-string">"de"</span>) {
       ogmetalang = <span class="hljs-string">"de_DE"</span>  
    } <span class="hljs-keyword">else</span> {
        ogmetalang = <span class="hljs-string">"en_US"</span>  
    }

    img = msg.posts[i].imgurl.split(<span class="hljs-string">'.'</span>)[<span class="hljs-number">0</span>]+<span class="hljs-string">".webp"</span>;

    ogmeta = <span class="hljs-string">`
    <meta property="og:type" content="website">
    <meta property="og:locale" content="`</span>+ogmetalang+<span class="hljs-string">`">
    <meta property="og:site_name" content="niklas-stephan.de">
    <link rel="canonical" href="`</span>+msg.baseurl+<span class="hljs-string">`/posts/`</span>+msg.posts[i].language+<span class="hljs-string">`/`</span>+msg.posts[i].distFilename+<span class="hljs-string">`">
    <meta name="description" content="`</span>+msg.posts[i].excerpt+<span class="hljs-string">`">
    <meta property="og:title" content="`</span>+msg.posts[i].title+<span class="hljs-string">`">
    <meta property="og:description" content="`</span>+msg.posts[i].excerpt+<span class="hljs-string">`">
    <meta property="og:url" content="`</span>+msg.baseurl+<span class="hljs-string">`/posts/`</span>+msg.posts[i].language+<span class="hljs-string">`/`</span>+msg.posts[i].distFilename+<span class="hljs-string">`">
    <meta property="og:image" content="`</span>+msg.baseurl+<span class="hljs-string">`/media/full/`</span>+img+<span class="hljs-string">`">
    <meta property="og:image:secure_url" content="`</span>+msg.baseurl+<span class="hljs-string">`/media/full/`</span>+img+<span class="hljs-string">`">
    <meta name="twitter:card" content="summary">
    <meta name="twitter:description" content="`</span>+msg.posts[i].excerpt+<span class="hljs-string">`">
    <meta name="twitter:title" content="`</span>+msg.posts[i].title+<span class="hljs-string">`">
    <meta name="twitter:image" content="`</span>+msg.baseurl+<span class="hljs-string">`/media/full/`</span>+img+<span class="hljs-string">`">`</span>

    postdate = <span class="hljs-string">'<small class="c-gray pb-3" id="postdate">'</span>+msg.posts[i].date+<span class="hljs-string">'</small>'</span>;

    data = <span class="hljs-string">""</span>;
    data = msg.templates[<span class="hljs-string">"post.html"</span>];

    data = data.replace(<span class="hljs-string">"<!-- html head from head.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"head.html"</span>]);
    data = data.replace(<span class="hljs-string">"<!-- Top Navigation from navbar.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"navbar.html"</span>]);
    data = data.replace(<span class="hljs-string">"<!-- footer navigation from footer.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"footer.html"</span>]);
    data = data.replace(<span class="hljs-string">"<!-- Javascript from script.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"script.html"</span>]);

    data = data.replace(<span class="hljs-string">"<!-- mardown content from posts -->"</span>,msg.posts[i].distContent);
    data = data.replace(<span class="hljs-string">"<!-- Post Headline -->"</span>, msg.posts[i].title);
    data = data.replace(<span class="hljs-string">"<!-- postdate -->"</span>, postdate);
    data = data.replace(<span class="hljs-string">"<!-- Post Image -->"</span>, <span class="hljs-string">'<img src="https://niklas-stephan.de/media/thumb/'</span>+img+<span class="hljs-string">'" class="img-fluid mb-2" alt="postImage">'</span>);
    data = data.replace(<span class="hljs-string">"<!-- PAGE TITLE -->"</span>,msg.posts[i].title);
    data = data.replace(<span class="hljs-string">"<!-- meta tags -->"</span>,ogmeta);

    msg.dist.posts[msg.posts[i].distFilename] = data;
}

<span class="hljs-keyword">return</span> msg;
```

### `create blog.html` (function)

Overview page: unique categories collected from all published posts, each assigned one of eight fixed CSS colors, then cards for every published post (title, excerpt, date, thumbnail). Result `msg.dist.blog`.

```javascript
<span class="hljs-comment">//get categories from all posts and extract unique ones</span>

<span class="hljs-keyword">var</span> categories = [];
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i=<span class="hljs-number">0</span> ; i<msg.posts.length;i++) {
    <span class="hljs-keyword">if</span> (msg.posts[i].published == <span class="hljs-literal">true</span>) {
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> j = <span class="hljs-number">0</span>; j < msg.posts[i].keywords.length; j++) {
            categories.push(msg.posts[i].keywords[j]);
        }
    }
}
<span class="hljs-keyword">var</span> uniqueCategories = [...new <span class="hljs-built_in">Set</span>(categories)];

<span class="hljs-comment">// define a color to stick for each category</span>

<span class="hljs-keyword">const</span> colorcat = {};
<span class="hljs-keyword">var</span> catcolors = [<span class="hljs-string">"green"</span>, <span class="hljs-string">"red"</span>, <span class="hljs-string">"blue"</span>, <span class="hljs-string">"orange"</span>, <span class="hljs-string">"yellow"</span>, <span class="hljs-string">"pink"</span>, <span class="hljs-string">"purple"</span>,<span class="hljs-string">"indigo"</span>];
<span class="hljs-keyword">var</span> c=<span class="hljs-number">0</span>;
 <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-keyword">of</span> uniqueCategories) {
      colorcat[key] = catcolors[c];
      c = c+<span class="hljs-number">1</span>;
 }

<span class="hljs-comment">//generate and set html for categorie selection</span>

<span class="hljs-keyword">var</span> cathtml = <span class="hljs-string">""</span>;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> k = <span class="hljs-number">0</span>; k < uniqueCategories.length; k++) {

    cathtml = cathtml + <span class="hljs-string">`<button data-filter=".cat-`</span>+uniqueCategories[k]+<span class="hljs-string">`" type="button" 
    onclick="sort()" class="btn bg-`</span> + catcolors[k] + <span class="hljs-string">` c-white me-2">`</span> + uniqueCategories[k]+<span class="hljs-string">`</button>`</span>;
}
msg.dist.blog = msg.templates[<span class="hljs-string">"blog.html"</span>].replace(<span class="hljs-string">"<!-- CATEGORIES -->"</span>, cathtml);

<span class="hljs-comment">// get card content from all posts and generate html</span>

<span class="hljs-keyword">var</span> posthtml = <span class="hljs-string">""</span>;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> l = <span class="hljs-number">0</span>; l < msg.posts.length; l++) {
    <span class="hljs-keyword">if</span> (msg.posts[l].published == <span class="hljs-literal">true</span>) {

    <span class="hljs-keyword">var</span> link = msg.posts[l].filename.slice(<span class="hljs-number">0</span>, <span class="hljs-number">-3</span>)+<span class="hljs-string">".html"</span>;

    <span class="hljs-comment">//get color for current post</span>
    <span class="hljs-keyword">var</span> postcolor = <span class="hljs-string">""</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-keyword">in</span> colorcat) {
        <span class="hljs-keyword">if</span> (key == msg.posts[l].keywords[<span class="hljs-number">0</span>]) {
            postcolor = colorcat[key];
        }
    }

    imgurl = msg.posts[l].imgurl.split(<span class="hljs-string">'.'</span>)[<span class="hljs-number">0</span>]+<span class="hljs-string">".webp"</span>;

    posthtml = posthtml + <span class="hljs-string">`
    <div class="col-sm-6 col-lg-4 my-4 filterDiv cat-`</span>+msg.posts[l].keywords[<span class="hljs-number">0</span>]+<span class="hljs-string">` lang-`</span>+ msg.posts[l].language + <span class="hljs-string">`">
        <span class="date hidden d-none">`</span>+ msg.posts[l].date + <span class="hljs-string">`</span>
        <span class="name hidden d-none">`</span>+ msg.posts[l].title + <span class="hljs-string">`</span>
        <div onclick="goto('`</span>+ link+ <span class="hljs-string">`','blog')" class="card h-100 d-flex align-items-center bo-`</span>+postcolor+<span class="hljs-string">`">
            <div class="card-header bg-`</span>+ postcolor + <span class="hljs-string">`">`</span> + msg.posts[l].keywords[<span class="hljs-number">0</span>] + <span class="hljs-string">`</div>
            <div class="card-img-wrapper d-flex align-items-center">
                <img src="media/thumb/`</span>+ imgurl + <span class="hljs-string">`"
                    class="card-img-top" alt="iot">
            </div>
            <div class="card-body">
                <h5 class="card-title">`</span>+ msg.posts[l].title + <span class="hljs-string">`</h5>
                <p class="card-text">
                    `</span>+ msg.posts[l].excerpt + <span class="hljs-string">`
                </p>
            </div>
            <div class="card-footer small text-center c-gray pdate">
                `</span>+msg.posts[l].date+<span class="hljs-string">`
            </div>
        </div>
    </div>
    `</span>;
    }
}

msg.dist.blog = msg.dist.blog.replace(<span class="hljs-string">"<!-- POSTS -->"</span>, posthtml);

<span class="hljs-keyword">var</span> ogmetalang = <span class="hljs-string">"de_DE"</span>;
<span class="hljs-keyword">var</span> ogmeta = <span class="hljs-string">`
<meta property="og:type" content="website">
<meta property="og:locale" content="`</span>+ogmetalang+<span class="hljs-string">`">
<meta property="og:site_name" content="niklas-stephan.de">
<link rel="canonical" href="`</span>+msg.baseurl+<span class="hljs-string">`/blog.html">
<meta name="description" content="Projekte und Posts aus der Welt von IoT, Musik und mehr">
<meta property="og:title" content="Projects & Blog - niklas-stephan.de">
<meta property="og:description" content="Projekte und Posts aus der Welt von IoT, Musik und mehr">
<meta property="og:url" content="`</span>+msg.baseurl+<span class="hljs-string">`/blog.html">
<meta property="og:image" content="`</span>+msg.baseurl+<span class="hljs-string">`/assets/img/me_logo.webp">
<meta property="og:image:secure_url" content="`</span>+msg.baseurl+<span class="hljs-string">`/assets/img/me_logo.webp">
<meta name="twitter:card" content="summary">
<meta name="twitter:description" content="Projekte und Posts aus der Welt von IoT, Musik und mehr">
<meta name="twitter:title" content="Projects & Blog - niklas-stephan.de">
<meta name="twitter:image" content="`</span>+msg.baseurl+<span class="hljs-string">`/assets/img/me_logo.webp">`</span>

msg.dist.blog = msg.dist.blog.replace(<span class="hljs-string">"<!-- meta tags -->"</span>,ogmeta);
msg.dist.blog = msg.dist.blog.replace(<span class="hljs-string">"<!-- html head from head.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"head.html"</span>]);
msg.dist.blog = msg.dist.blog.replace(<span class="hljs-string">"<!-- Top Navigation from navbar.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"navbar.html"</span>]);
msg.dist.blog = msg.dist.blog.replace(<span class="hljs-string">"<!-- footer navigation from footer.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"footer.html"</span>]);
msg.dist.blog = msg.dist.blog.replace(<span class="hljs-string">"<!-- Javascript from script.html snipppet -->"</span>,msg.snippets[<span class="hljs-string">"script.html"</span>]);
msg.dist.blog = msg.dist.blog.replace(<span class="hljs-string">"<!-- PAGE TITLE -->"</span>,<span class="hljs-string">"Projekte & Blog"</span>);

<span class="hljs-keyword">return</span> msg;
```

### `write files` (function)

Writes everything from `msg.dist` to `/dist/`; `await` keeps steps sequential and the dist directory is emptied first so no "file ghosts" of previous deployments survive.

```javascript
<span class="hljs-keyword">await</span> fse.emptyDir(<span class="hljs-string">'/dist'</span>).then(<span class="hljs-function"><span class="hljs-params">()</span> =></span> {
    fse.mkdirSync(<span class="hljs-string">'/dist/media'</span>);
    fse.mkdirSync(<span class="hljs-string">'/dist/media/full'</span>);
    fse.mkdirSync(<span class="hljs-string">'/dist/media/thumb'</span>);
    fse.mkdirSync(<span class="hljs-string">'/dist/posts'</span>);
});

<span class="hljs-keyword">await</span> fse.copySync(<span class="hljs-string">'/src/assets/'</span>, <span class="hljs-string">'/dist/assets/'</span>);
<span class="hljs-keyword">await</span> fse.copySync(<span class="hljs-string">'/src/templates/robots.txt'</span>, <span class="hljs-string">'/dist/robots.txt'</span>);

<span class="hljs-keyword">await</span> fse.writeFileSync(<span class="hljs-string">'/dist/index.html'</span>, msg.dist.index);
<span class="hljs-keyword">await</span> fse.writeFileSync(<span class="hljs-string">'/dist/blog.html'</span>, msg.dist.blog);
<span class="hljs-keyword">await</span> fse.writeFileSync(<span class="hljs-string">'/dist/404.html'</span>, msg.dist.errorpage);
<span class="hljs-keyword">await</span> fse.writeFileSync(<span class="hljs-string">'/dist/privacy-policy.html'</span>, msg.dist.privacy);
<span class="hljs-keyword">await</span> fse.writeFileSync(<span class="hljs-string">'/dist/search.html'</span>, msg.dist.search);
<span class="hljs-keyword">await</span> fse.writeFileSync(<span class="hljs-string">'/dist/sitemap.xml'</span>, msg.dist.sitemap);
<span class="hljs-keyword">await</span> fse.writeFileSync(<span class="hljs-string">'/dist/searchindex.json'</span>, msg.dist.searchindex);

<span class="hljs-keyword">const</span> postfilefolder = <span class="hljs-string">"/dist/posts/"</span>

<span class="hljs-keyword">await</span> <span class="hljs-built_in">Object</span>.entries(msg.dist.posts).forEach(<span class="hljs-function"><span class="hljs-params">item</span> =></span> {
  <span class="hljs-keyword">var</span> [key, value] = item;
  fse.writeFileSync(postfilefolder+key, value)
});

<span class="hljs-keyword">return</span> msg;
```

### `convert media files` (function)

`/src/media/` (one subfolder level deep) → `/dist/media/full/` + `/dist/media/thumb/` as `.webp` with a 440 px thumbnail, using `sharp` and `fs-extra`; non-image formats are copied unchanged. The write calls deliberately run **without** `await` – loops start the next file before the write completes, which speeds up deployments by a large factor at the accepted risk that a media file may not be finished when the deployment reports done.

```javascript
<span class="hljs-keyword">var</span> srcpath = <span class="hljs-string">'/src/media/'</span>;
<span class="hljs-keyword">var</span> fullpath = <span class="hljs-string">'/dist/media/full/'</span>;
<span class="hljs-keyword">var</span> thumbpath = <span class="hljs-string">'/dist/media/thumb/'</span>;
<span class="hljs-keyword">const</span> filetypes = [<span class="hljs-string">"jpg"</span>, <span class="hljs-string">"jpeg"</span>, <span class="hljs-string">"png"</span>, <span class="hljs-string">"gif"</span>, <span class="hljs-string">"webp"</span>];
<span class="hljs-keyword">const</span> mediafiles = fse.readdirSync(srcpath);
<span class="hljs-keyword">const</span> alength = mediafiles.length;
<span class="hljs-keyword">var</span> mediafile = <span class="hljs-string">""</span>;
<span class="hljs-keyword">var</span> targetfile = <span class="hljs-string">""</span>;

<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i=<span class="hljs-number">0</span>; i<alength; i++) {
    mediafile = mediafiles[i];

    <span class="hljs-keyword">if</span> (mediafile.includes(<span class="hljs-string">"."</span>)) {
        targetfile = mediafile.split(<span class="hljs-string">'.'</span>)[<span class="hljs-number">0</span>];
        <span class="hljs-keyword">if</span> (filetypes.includes(mediafile.split(<span class="hljs-string">'.'</span>)[<span class="hljs-number">1</span>]) ) {
            sharp(srcpath+mediafile)
            .toFile(fullpath+targetfile+<span class="hljs-string">'.webp'</span>);
            sharp(srcpath+mediafile).resize({ <span class="hljs-attr">width</span>: <span class="hljs-number">440</span> })
            .toFile(thumbpath+targetfile+<span class="hljs-string">'.webp'</span>);
        } <span class="hljs-keyword">else</span> {
            fse.copySync(srcpath+mediafile, fullpath+mediafile)
        }

    } <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">var</span> subsrcpath = srcpath+mediafile+<span class="hljs-string">"/"</span>;
        <span class="hljs-keyword">var</span> subfullpath = fullpath+mediafile+<span class="hljs-string">"/"</span>;
        <span class="hljs-keyword">var</span> subthumbpath = thumbpath+mediafile+<span class="hljs-string">"/"</span>;
        <span class="hljs-keyword">var</span> submediafiles = fse.readdirSync(subsrcpath);
        <span class="hljs-keyword">var</span> blength = submediafiles.length;
        <span class="hljs-keyword">await</span> fse.mkdirSync(subfullpath);
        <span class="hljs-keyword">await</span> fse.mkdirSync(subthumbpath);

        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> j=<span class="hljs-number">0</span>; j<blength; j++) {
            submediafile = submediafiles[j];
            subtargetfile = submediafile.split(<span class="hljs-string">'.'</span>)[<span class="hljs-number">0</span>];

            <span class="hljs-keyword">if</span> (filetypes.includes(submediafile.split(<span class="hljs-string">'.'</span>)[<span class="hljs-number">1</span>]) ) {
                sharp(subsrcpath+submediafile)
                .toFile(subfullpath+subtargetfile+<span class="hljs-string">'.webp'</span>);

                sharp(subsrcpath+submediafile).resize({ <span class="hljs-attr">width</span>: <span class="hljs-number">440</span> })
                .toFile(subthumbpath+subtargetfile+<span class="hljs-string">'.webp'</span>);
            } <span class="hljs-keyword">else</span> {
                fse.copySync(subsrcpath+submediafile, subfullpath+submediafile)
            }
        }
    }
}

<span class="hljs-keyword">return</span> msg;
```

### `finish` (function)

Duration and timestamp recorded to `/dist/deploy.log`; routing depends on trigger (manual → debug node only, webhook → debug + `http out`).

```javascript
<span class="hljs-keyword">var</span> endtime = <span class="hljs-built_in">Date</span>.now();
<span class="hljs-keyword">var</span> duration = endtime-msg.starttime;
duration = duration;
<span class="hljs-keyword">let</span> yourDate = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>()
<span class="hljs-keyword">var</span> depdate = yourDate.toISOString();

<span class="hljs-keyword">var</span> log = <span class="hljs-string">"Deployment duration: "</span>+duration+<span class="hljs-string">" ms \n"</span>;
log = log+<span class="hljs-string">"Deployment timestamp: "</span>+depdate;
msg.statusCode = <span class="hljs-number">200</span>;

fse.writeFileSync(<span class="hljs-string">'/dist/deploy.log'</span>, log);
msg.payload = log;

<span class="hljs-keyword">if</span> (msg.type != <span class="hljs-string">"manual"</span>) {
    <span class="hljs-keyword">return</span> [msg,msg];
} <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">return</span> [msg,<span class="hljs-literal">null</span>];
}
```

### `msg` & `http` (debug, http out)

Clean conclusion: `http out` returns the defined body to the calling webhook, the debug node shows the full deployment payload when activated.

![Webhook response](https://niklas-stephan.de/user/pages/wiki/02.self-hosted/ncms/09-ncms-webhook.webp)

![Node-RED debug output](https://niklas-stephan.de/user/pages/wiki/02.self-hosted/ncms/10-ncms-debug.webp)

## Performance

![Page load of the start page](https://niklas-stephan.de/user/pages/wiki/02.self-hosted/ncms/04-ncms-pageload.webp)

![Lighthouse result](https://niklas-stephan.de/user/pages/wiki/02.self-hosted/ncms/06-ncms-lighthouse.webp)

## Known limitations

- Frontend details (placeholder comments, CSS class names) are referenced inside the backend flow code – adopting the sources requires reading the frontend as well.
- Single-node Node-RED deployment: no build parallelism, and media writes are fire-and-forget (see above).

## Links

- Sources: <https://github.com/handtrixx/ncms>
- Node-RED: <https://nodered.org/> · container image: <https://hub.docker.com/r/nodered/node-red>
- Nginx Proxy Manager: <https://nginxproxymanager.com/>
- Bootstrap 5: <https://getbootstrap.com/>
- markdown-it: <https://markdown-it.github.io/>
- fs-extra: <https://github.com/jprichardson/node-fs-extra>
- sharp: <https://sharp.pixelplumbing.com/>

 self-hostedcmsnode-redown-projectretired

---

## Navigation

- Parent: [Self-hosted](https://niklas-stephan.de/en/wiki/self-hosted.md)
- Previous: [Matomo](https://niklas-stephan.de/en/wiki/self-hosted/matomo.md)
- Next: [Node-RED](https://niklas-stephan.de/en/wiki/self-hosted/node-red.md)
