📜 ⬆️ ⬇️

Node.JS: a library for modifying http responses

Some time ago, I wrote a site with a backend on Express /Node.JS. There was a problem with the minification of answers. I found many ready-made packages, but everyone had a problem - html was not minified after the templates. As a result, I decided to write my own small and native bicycle - the web-minify library , which allows you to embed a hook before sending it to the client and modify the response.

Package installation


npm i @dmitriym09/web-minify --save 

I think the best library description for a developer is code sample =)

Example


web-minify - middleware-function:

 const htmlminify = require('html-minifier').minify; const csso = require('csso').minify; const postcss = require('postcss'); const precss = require('precss'); const autoprefixer = require('autoprefixer'); const minify = require('web-minify'); app.use(minify([ { contentType: /css/, minify: async (data, req, res) => { let resData = (await postcss([precss, autoprefixer]).process(data, { from: undefined })).css; resData = csso(resData).css; return resData; } } ])); 

In this example, all responses with a Content-Type containing the substring "css" will be intercepted. The response body is processed using csso , postcss , precss , autoprefixer . The contentType parameter is passed to a String (an entry of String.prototype.indexOf () will be searched for) or RegExp (RegExp.prototype.test ()). The minify parameter, the Function function (data: String, req: Request, res: Response), must return a String with a new body or Promise, which in turn is resolved to a String. If an exception is not caught, the client will receive a response of 500.
')
As already said, most of the existing popular libraries with similar functionality well minify static files. However, the minification of the responses generated in the code (for example, html template) does not work. One of the problems is that the response can be sent in parts, and for processing, you usually need complete data. Accordingly, it is necessary to intercept all sending to the user, collect and already at the end process and send. This must be taken into account when using web-minify : the terabyte file that you want to send to the client and which falls under the contentType will accumulate in memory.

Examples


HTML minification with html-minifier from unit tests


 const htmlminify = require('html-minifier').minify; it('HTML', (done) => { const app = createServer([minify([ { contentType: 'html', minify: (data) => { let res = htmlminify(data, { removeAttributeQuotes: true, collapseWhitespace: true, conservativeCollapse: false, decodeEntities: true, keepClosingSlash: false, preserveLineBreaks: false, preventAttributesEscapin, processConditionalComments: true, removeAttributeQuotes: true, removeComments: true, trimCustomFragments: true, useShortDoctype: true }); return res; } } ])], function(req, res) { res.setHeader('Content-Type', 'text/html; charset=utf-8'); res.end(`<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> </head> <body> <h1>Test</h1> <p>Test</p> </body>`); }); request(app) .get('/') .set('Accept', 'text/html; charset=utf-8') .expect('Content-Type', 'text/html; charset=utf-8') .expect('<!doctype html><html lang=en><head><meta charset=utf-8></head><body><h1>Test</h1><p>Test</p></body></html>') .expect(200) .end(done) }); 

JSON and response code modifications with Promise return from unit tests


 it('JSON', (done) => { const app = createServer([minify([ { contentType: /json/, minify: (data, req, res) => { return new Promise(function(resolve, reject) { try { res.statusCode = 456; let o = JSON.parse(data); o.dt = new Date('2018-09-28T11:05:13.492Z') resolve(JSON.stringify(o)) } catch(exc) { reject(exc) } }) } } ])], function(req, res) { res.setHeader('Content-Type', 'application/json; charset=utf-8'); res.end(JSON.stringify({a: 12})); }); request(app) .get('/') .set('Accept', 'applicatio3n/json; charset=utf-8') .expect('Content-Type', 'application/json; charset=utf-8') .expect('{"a":12,"dt":"2018-09-28T11:05:13.492Z"}') .expect(456) .end(done) }); 

Web-minify is available on github and in npm under the MIT license.

Thanks for attention! Criticism, suggestions and comments are welcome!

Source: https://habr.com/ru/post/425371/


All Articles