
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
console-request
Advanced tools
HTTP request logger middleware for node.js
A simple logging library for multi-level log for console and file.
$ npm i console-request -s
Take a look into the usage section for a detailed example.
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.
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);
console-request accepts these properties in the options object.
log the header, request and response in the console (default: true), it accepts boolean
app.use(consoleRequest({
console : false
}));
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,
}));
write the header of the request. it accepts boolean or object (defalut: true)
app.use(consoleRequest({
header: false,
}));
OR
app.use(consoleRequest({
header: { pretty: false, excludeKeys: ["token"] },
}));
write the request body of the request. it accepts boolean or object (defalut: true)
app.use(consoleRequest({
request: false,
}));
OR
app.use(consoleRequest({
request: { pretty: false, excludeKeys: ["password"] },
}));
write the response of the api. it accepts boolean or object (defalut: true)
app.use(consoleRequest({
response: false,
}));
OR
app.use(consoleRequest({
response: { pretty: false, excludeKeys: ["password"] },
}));
write the custom message of the api. it accepts string
app.use(consoleRequest({
message: "user router",
}));
exclude some urls to write
app.use(consoleRequest({
excludeURLs: ["/auth"] ,
}));
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!')
})
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!')
})
FAQs
log request body and response
We found that console-request demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.