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

Views

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 Write and Run Your Firs …

How To Write and Run Your First Program in Node.js. Node.js is a popular open-source runtime environment that can execute JavaScript outside of the browser using the V8 JavaScript engine, which is the same engine used to power the Google Chrome web b …

read more
Views

Getting Started with PM2, the …

Getting Started with PM2, the Node.js Process Manager. PM2, or Process Manager 2 is an incredibly versatile production process manager written in Node.js.Restarting after crashes: PM2 allows us to keep processes running until the heat death of the un …

read more
Views