const crypto = require('crypto'); const hash = crypto.createHash('sha384'); function generateCSPHash(script) { const data = hash.update(script, 'utf-8'); return ( 'sha384-' + data .digest('base64') .replace(/=/g, '') .replace(/\+/g, '-') .replace(/\//g, '_') ); } script = " \ const button = document.querySelector('#hello2'); \ button.addEventListener('click', () => { \ const h1 = document.createElement('h1'); \ h1.textContent = 'Hello World 2!'; \ document.body.appendChild(h1); \ }); \ "; var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end(generateCSPHash(script)); }).listen(8080);