Simple Node.js Webhook

updated: 10.05.2022

Simple webhook for restart PM2 process with auth and params.

const secret = 'mySecret';
const http = require('http');
const exec = require('child_process').exec;

const PM2_CMD = 'pm2 restart nuxt-frontend';

http
    .createServer(function(req, res) {
        req.on('data', function(chunk) {
            if (req.headers['authorization'] === secret) {
                if(req.headers['referer'] === 'frontend-deploy') {
                    exec(`${PM2_CMD}`);
                }
            }
        });
        res.end();
    })
    .listen(8900);