Code Monkey home page Code Monkey logo

console-log-server's Introduction

console-log-server

Logs all requests to command line console (stdout) and responds 200 OK.

Useful for quickly viewing what kind of requests your app is sending.

Usage

If you have npm >= v5.2 (comes bundled with node >= v8.2.0) you can use npx to run it directly:

$ npx console-log-server -p 8000

If you have older node (all version >= v0.10 supported!) or just prefer the old fashioned way:

$ npm install console-log-server --global
$ console-log-server -p 8000

Demo

Command line options

  Logs all http requests to console

  Usage
    $ console-log-server

  Options
    --port, -p Port Number
    --hostname, -h Host name. You can provide multiple hostname flags (with optional matching port flags) to listen many hostnames. 
    --proxy, -P Host(s) to proxy the request to using https://www.npmjs.com/package/express-http-proxy. Syntax: [<path>>]<url>. You can provide different proxies for separate paths.
    --response-code, -c Response response code (ignored if proxied)
    --response-body, -b Response content (ignored if proxied)
    --response-header, -H Response header (ignored if proxied)
    --log-response, -r Log also the response. Enabled by default only for proxied requests. Logged response is fully read to a buffer which might change your api behaviour since response is not streamed directly to client, consider turning off if that is a problem.
    --no-color
    --version
    --date-format, -d Date format supported by https://www.npmjs.com/package/dateformat (default "yyyy-mm-dd'T'HH:MM:sso")
    --help
    --default-cors, -C Add "default" cors using https://www.npmjs.com/package/cors default values. By default only enabled for non-proxied responses. Turn on to enable also for proxy responses, turn off to disable completely.

  Examples

    # basic usage
    $ console-log-server -p 3000

    # customized response
    $ console-log-server -p 3000 -c 201 -b "cool type content" --response-header='Content-Type:application/cool' --response-header='key:value'

    # Log date with UTC date format instead of local with offset
    $ console-log-server -d "isoUtcDateTime"

    # Proxy the request to other host. Response will be the actual response from the proxy. 
    $ console-log-server -P http://api.example.com

    # Proxy the requests to multiple hosts based on paths.
    $ console-log-server --proxy="/api/1>http://api-1.example.com" --proxy="/api/2>http://api-2.example.com"

    # Proxy the request to path under other host. Response will be the actual response (with cors headers injected) from the proxy.
    $ console-log-server -P http://api.example.com/v1/cats -C yes

    # Turn off response logging
    $ console-log-server -r no

    # Turn on response logging for all requests
    $ console-log-server -r yes

    # Don't add default (allow all) cors headers at all
    $ console-log-server -C no

    # Start server to your local IP and localhost. Might be useful when debugging devices connected to your own machine. Ports can be given for each hostname with --port flag(s).
    $ console-log-server -h localhost -h 192.168.0.2 

Legacy Node.js support

Are you stuck with old unmaintained node.js version filled with security holes? You know you should upgrade, but you have your reasons. Don't worry, console-log-server is not here to judge, but to help (although you should really upgrade and definitely not run anything unmaintained in production, so judging a little bit here).

Currently all node.js version >= v0.10 are supported. This is done by transpiling to ES5 using babel and requiring core-js for missing types on standalone mode.

Development/forking/building of console-log-server itself requires node >= v15 though and you should always use latest stable node for that.

Only for debugging/development use

Only use this tool for ad-hoc local testing. NEVER run console-log-server in production for multiple reasons:

  • It logs all data as is without filters. Including passwords, tokens, user names etc
  • It will make performance lot worse
  • It might use old/deprecated libraries or node.js version with vulnerabilities

Library use

If you are using console-log-serveras a library and use very old node without newer JS types like Map or Promise you need to polyfill them. Otherwise you get errors like:

"ReferenceError: Promise is not defined"

This is mainly problem with node 0.10 but you could in theory get some errors with node < v6.

Easiest way to fix these is to just load core-js before console-log-server

require('core-js');
var consoleLogServer = require('console-log-server');

Running tests

All tests with installed node version:

npm test

Updating snapshots:

npm test -- --update-snapshots

Compatibility tests for older node versions

npm test:compatibility:all

console-log-server's People

Contributors

avantassel avatar jamonkko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

avantassel

console-log-server's Issues

Does not work on Node 0.x

Logger uses String.repeat but it does not exist on Node 0.x and the server crashes:

node_modules/console-log-server/dist/logging.js:29
        var divLine = color('*'.repeat(text.length));
                                ^
    TypeError: undefined is not a function
        at divider (/home/vesse/temp/jarkkuli/node_modules/console-log-server/dist/logging.js:29:29)
        at exports.default (/home/vesse/temp/jarkkuli/node_modules/console-log-server/dist/logging.js:43:20)
        at ServerResponse.<anonymous> (/home/vesse/temp/jarkkuli/node_modules/console-log-server/dist/router.js:79:29)
        at ServerResponse.emit (events.js:104:17)
        at finish (_http_outgoing.js:551:10)
        at afterWrite (_stream_writable.js:361:3)
        at _stream_writable.js:349:9
        at process._tickCallback (node.js:355:11)

Script crashes when submitting a POST request

This is just the tool I've been looking for, thanks! I've been trying to find a way to debug outgoing API calls from my web app, and this looks really helpful.

Unfortunately the console-log-server keeps crashing when I send it a POST request using curl (either via command line or PHP-curl). Example:

curl --data "test=1" localhost:3000

Here is the crash message. Do you have any idea about what might be causing this? It seems to be that the script cannot correctly determine the bodyType and so it throws an exception.

I installed the app from npm, if that makes a difference.

console-log-server listening on http://localhost:3000

*************************
POST /
headers:
  host:           localhost:3000
  user-agent:     curl/7.47.0
  accept:         */*
  content-length: 10
  content-type:   application/x-www-form-urlencoded
query: (empty)
C:\Dev\____\node_modules\console-log-server\dist\logging.js:94
      throw new Error('Internal Error! bodyType not set!');
      ^

Error: Internal Error! bodyType not set!
    at exports.default (C:\Dev\______\node_modules\console-log-server\dist\logging.js:94:13)
    at ServerResponse.<anonymous> (C:\Dev\______\node_modules\console-log-server\dist\router.js:79:29)
    at emitNone (events.js:91:20)
    at ServerResponse.emit (events.js:185:7)
    at finish (_http_outgoing.js:593:10)
    at afterWrite (_stream_writable.js:388:3)
    at _combinedTickCallback (internal/process/next_tick.js:80:20)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.