How to display access log info at console in Hapi Js.

For displaying access log we need to use some plugins of Hapi Js framework which are already in market. E.g good, good-console and good-squeeze.

So as you know How to install Hapi Js. If you have already installed it then start with plugins otherwise read my previous blog regarding How to install Hapi Js.

After installed hapi install above 3 defined plugins with following command.

 
npm install --save good 
npm install --save good-console 
npm install --save good-squeeze 

As you will run these command all the three plugins will installed and will save the dependency in package.json file.

Now then update the server.js file as here.

 
'use strict';  

const Hapi = require('hapi'); 
const Good = require('good');  

const server = new Hapi.Server(); 

server.connection({ 
port: 3000, 
host: 'localhost' 
});  

server.route({     
method: 'GET',     
path: '/',     
handler: function (request, reply) {         
reply('Hello, world!');     
} 
});  

server.register({     
register: Good,     
options: {         
reporters: {             
console: [{                 
module: 'good-squeeze',                 
name: 'Squeeze',                 
args: [{                     
response: '*',                     
log: '*'                 
}]             
}, 
{                 
module: 'good-console'             
}, 'stdout']         
}     
} 
}, (err) => {      
if (err) {         
throw err; // something bad happened loading the plugin     
}      

server.start((err) => {          
if (err) {             
throw err;         
}         

server.log('info', 'Server running at: ' + server.info.uri);     
}); 
});  

Now when the server is started you'll see:

 140625/143008.751, [log,info], data: Server running at: http://localhost:3000 

And if we visit http://localhost:3000/ in the browser, you'll see

 
140625/143205.774, [response], http://localhost:3000: get / {} 200 (10ms) 

So this is the example for display access log info

How to Deploy Python Application on Kubernetes with Okteto

Deploying a Python application on Kubernetes with Okteto involves setting up your Kubernetes cluster, creating a Docker container for your Python application, and using Okteto to deploy the application on your Kubernetes cluster. Okteto is a platform …

read more

Explain the concept of streams in Node.js. How are they used, and what are …

In Node.js, streams are powerful mechanisms for handling data transfer, especially when dealing with large amounts of data or performing I/O operations. Streams provide an abstraction for handling data in chunks, rather than loading entire datasets i …

read more