@mashthekeys/response-console

1.1.1 • Public • Published

@mashthekeys/response-console

NPM

Creates a buffered console object on a Response object, which outputs to its parent console when the response finishes. Comes with middleware for server logging by request.

Intended to help logs remain coherent in high-concurrency applications, where asynchronous processes to load files or query databases can otherwise result in jumbled logs.

Examples

Basic usage

const responseConsole = require("@mashthekeys/response-console");

createServer(function (req, res) {
  responseConsole(res, console)

  console.log("This output appears immediately.")

  res.console.log("This will only output to console when the response is finished.")

  someAsyncProcess(req, res)
    .then(data => {
      res.console.log("Process completed, response is about to finish.")

      res.end(data)
    })
});

Outputting to a custom console

const responseConsole = require("@mashthekeys/response-console");

const logger = require("some-package")();

createServer(function(req, res) {
  responseConsole(res, logger)
  
  res.console.log("This will be appended to the log when the response is finished.")

  logger.log("This log message appears immediately.")
  
  someAsyncProcess().then(data => res.end(data))
});

Log every request to console

const app = require("express")();
const responseConsole = require("@mashthekeys/response-console");

app.all("*", responseConsole.middleware(console))

Package Sidebar

Install

npm i @mashthekeys/response-console

Weekly Downloads

1

Version

1.1.1

License

ISC

Unpacked Size

3.67 kB

Total Files

3

Last publish

Collaborators

  • mashthekeys