Koa.js Session Handling in Browser Using Koa-session Library Full Tutorial Example For Beginners


Welcome folks today in this blog post we will be implementing session handling in koa.js using koa-session library in browser. 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-session



After installing this library you need to make an index.js file and copy paste the following code






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

app.keys = ['Shh, its a secret!'];
app.use(session(app));  // Include the session middleware

app.use(function *(){
   var n = this.session.views || 0;
   this.session.views = ++n;
   if(n === 1)
      this.body = 'Welcome here for the first time!';
      this.body = "You've visited this page " + n + " times!";


Leave a Reply