Koa.js Compressing Data in App in Browser Using Koa-compress Middleware Library Full Project For Beginners

 

Welcome folks today in this blog post we will be implementing compression of data inside app in browser using koa-compress middleware library in koa.js.All the full source code of the application is shown below.

 

 

 

Get Started

 

 

 

In order to get started you need to install the below library using the npm command as shown below

 

 

npm i koa-compress

 

 

After installing this library you can make an app.js file and copy paste the following code

 

 

See also  Build a Drawing Paint HTML5 Canvas App to Draw Lines,Rectangles Using Pencil in Browser Using Vanilla Javascript Full Project For Beginners

app.js

 

 

var koa = require('koa');
var router = require('koa-router');
var app = koa();

var Pug = require('koa-pug');
var pug = new Pug({
   viewPath: './views',
   basedir: './views',
   app: app //Equivalent to app.use(pug)
});

app.use(compress({
   filter: function (content_type) {
      return /text/i.test(content_type)
   },
   threshold: 2048,
   flush: require('zlib').Z_SYNC_FLUSH
}));

var _ = router(); //Instantiate the router

_.get('/', getRoot);

function *getRoot(next){
   this.render('index');
}

app.use(_.routes()); //Use the routes defined using the router
app.listen(3000);

Leave a Reply