npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

console-request

v0.0.4

Published

log request body and response

Downloads

3

Readme

console-request

HTTP request logger middleware for node.js

Table of Contents

Why?

A simple logging library for multi-level log for console and file.

Installation

$ npm i console-request -s

Functions

Take a look into the usage section for a detailed example.

console-request

Note: Use this Middleware below body-parser Middleware.

This Middleware Takes an object as an parameter and logs the request body, request headers and response.

Usage

Example 1
Note: it will logs all API

import * as consoleRequest from 'console-request';
import express from 'express';
import bodyParser from 'body-parser';

const app = express();

// must parse body before console-request as body will be logged
app.use(bodyParser.json());

// consoleRequest middleware logs the request 
app.use(consoleRequest());

Example 2
Note: it will logs Particular router

import * as consoleRequest from 'console-request';
import express from 'express';
import bodyParser from 'body-parser';
import * as userRouter from './user';

const app = express();

// must parse body before console-request as body will be logged
app.use(bodyParser.json());

// consoleRequest middleware logs only user Router 
app.use("/user", consoleRequest(), userRouter);

Options

console-request accepts these properties in the options object.

console

log the header, request and response in the console (default: true), it accepts boolean

app.use(consoleRequest({
  console : false
}));
stream

write the header, request and response in file. it accepts writable stream

var logStream = fs.createWriteStream(path.join(__dirname, 'dev.log'), { flags: 'a' })

app.use(consoleRequest({
  stream: logStream,
}));
header

write the header of the request. it accepts boolean or object (defalut: true)

  • pretty: To formate header object (default: true)
  • excludeKeys: it accepts array of key that can be excluded
app.use(consoleRequest({
  header: false,
}));

OR

app.use(consoleRequest({
  header: { pretty: false, excludeKeys: ["token"] },
}));
request

write the request body of the request. it accepts boolean or object (defalut: true)

  • pretty: To formate request body object (default: true)
  • excludeKeys: it accepts array of key that can be excluded
app.use(consoleRequest({
  request: false,
}));

OR

app.use(consoleRequest({
  request: { pretty: false, excludeKeys: ["password"] },
}));
response

write the response of the api. it accepts boolean or object (defalut: true)

  • pretty: To formate response object (default: true)
  • excludeKeys: it accepts array of key that can be excluded
app.use(consoleRequest({
  response: false,
}));

OR

app.use(consoleRequest({
  response: { pretty: false, excludeKeys: ["password"] },
}));
message

write the custom message of the api. it accepts string

app.use(consoleRequest({
  message: "user router",
}));
excludeURLs

exclude some urls to write

app.use(consoleRequest({
  excludeURLs: ["/auth"] ,
}));

write logs to a file

single file

Sample app that will log all requests in the to the file access.log.

var express = require('express')
var fs = require('fs')
var consoleRequest = require('console-request')
var path = require('path')

var app = express()

// create a write stream (in append mode)
var logStream = fs.createWriteStream(path.join(__dirname, 'dev.log'), { flags: 'a' })

app.use(consoleRequest({
  stream: logStream,
}));

app.get('/', function (req, res) {
  res.send('hello, world!')
})

log file rotation

Sample app that will log all requests in the log file per day in the log/ directory using the rotating-file-stream module.

var express = require('express')
var consoleRequest = require('console-request')
var path = require('path')
var rfs = require('rotating-file-stream') // version 2.x

var app = express()

// create a rotating write stream
var accessLogStream = rfs.createStream('access.log', {
  interval: '1d', // rotate daily
  path: path.join(__dirname, 'log')
})

// setup the logger
app.use(consoleRequest({
  stream: accessLogStream,
}));
app.get('/', function (req, res) {
  res.send('hello, world!')
})