HTML Minifier Node.js Example
Check the example on how to use
Node.js
to minify a HTML hardcoded string and output to stdout:const querystring = require('querystring');
const https = require('https');
const query = querystring.stringify({
input : '<input type="text" />',
});
const req = https.request(
{
method : 'POST',
hostname : 'www.toptal.com',
path : '/developers/html-minifier/api/raw',
},
function(resp) {
// if the statusCode isn't what we expect, get out of here
if ( resp.statusCode !== 200 ) {
console.log('StatusCode=' + resp.statusCode);
return;
}
resp.pipe(process.stdout);
}
);
req.on('error', function(err) {
throw err;
});
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
req.setHeader('Content-Length', query.length);
req.end(query, 'utf8');Save the code to a file named
minify.js
and run the following command:node minify.js
Output:
<input>