WebFlow Publish Webhook

Install instructions:

Webhook server and production domain should be on the same server within same filesystem. Nginx server setup should point to /domains folder.

node webflow-updater.js

webflow-updater.js:

const http = require("http");
const https = require("https");
const fs = require('fs');

const host = '0.0.0.0';
const port = 3777;

const domains = {
    "www.domain.com": "domain-xyz098.webflow.io",   // state here your production domain and webflow.io temporary domain
};

const requestListener = function (req, res) {
    if (req.method == 'POST') {
        var body = '';
        req.on('data', function (data) {
            body += data;
        });
        req.on('end', function() {
            try {
                body = JSON.parse(body);
                for(var d in domains) {
                    console.log("Looking for " + domains[d])
                    if(body.domains.indexOf(domains[d]) !== -1) {
                        console.log("Updating " + domains[d]);
                        const path = "./domains/" + domains[d] + ".html";
                        const file = fs.createWriteStream(path);
                        const request = https.get("https://" + domains[d] + "/", function(response) {
                            response.pipe(file);
                            file.on("finish", function () {
                                file.close();
                                console.log("Download completed.");

                                fs.readFile(path, 'utf8', function (err,data) {
                                    console.log("Doing replacements.");
                                    if (err) {
                                        return console.log(err);
                                    }
                                    var result = data.replace(//g, '');
                                    result = result.replace(domains[d], d);

                                    fs.writeFile(path, result, 'utf8', function (err) {
                                        if (err) return console.log(err);
                                    });
                                    console.log("Done.");
                                });
                            });
                            file.on("error", function (e) {
                                file.close();
                                console.log("Error ", e);
                            })
                        });
                    }
                }
                //
                res.writeHead(200);
                res.end(" OK");
            }
            catch (e) {
                res.writeHead(400);
                res.end(" JSON ERROR")
            }
        });
    }
    else {
        res.writeHead(403);
        res.end(" REQUEST ERROR");
    }
};

const server = http.createServer(requestListener);
server.listen(port, host, () => {
    console.log(`Server is running on http://${host}:${port}`);
});