NodeJS

Table of contents

  1. Clone repository
  2. Run
  3. Configuration
  4. Loggers
  5. Examples

Clone repository

➜ git clone https://github.com/Ealenn/Echo-Server.git

Or download source code of latest release here

And install dependencies

➜ npm i

Run

# Run with node
➜ node ./src/webserver --port 8080
# Run with npm scriptPORT=8080 npm run start

Configuration

You can use environment variables or CLI arguments More information

  • With environment variables:
PORT=8080 npm run start
  • With CLI arguments:
➜ node ./src/webserver --port 8080 --enable:environment false

Loggers

More information

➜ node ./src/webserver \
    --port 8080 \
    --logs:seq:enabled true \
    --logs:seq:server http://localhost:5341

Examples

Custom responses

Query Header Content Conditions
?echo_code= X-ECHO-CODE HTTP code 200, 404 200 <= CODE <= 599
    404-401 or 200-500-301  
?echo_body= X-ECHO-BODY Body message  
?echo_env_body= X-ECHO-ENV-BODY The key of environment variable Enable environment true
?echo_header= X-ECHO-HEADER Response Header Lang: en-US Enable header true
?echo_time= X-ECHO-TIME Wait time in ms 0 <= TIME <= 60.000
?echo_file= X-ECHO-FILE Path of Directory or File Enable file true

You can change commands and conditions.

Custom HTTP Status Code

➜ curl -I --header 'X-ECHO-CODE: 404' localhost:8080
➜ curl -I localhost:8080/?echo_code=404

HTTP/1.1 404 Not Found
➜ curl -I --header 'X-ECHO-CODE: 404-300' localhost:8080
➜ curl -I localhost:8080/?echo_code=404-300

HTTP/1.1 404 Not Found
HTTP/1.1 300 Multiple Choices
for i in {1..10}do
➜    curl -I localhost:8080/?echo_code=200-400-500
➜ done

HTTP/1.1 500 Internal Server Error
HTTP/1.1 400 Bad Request
HTTP/1.1 200 OK
HTTP/1.1 500 Internal Server Error
HTTP/1.1 200 OK
HTTP/1.1 500 Internal Server Error

Custom Body

➜ curl --header 'X-ECHO-BODY: amazing' localhost:8080
➜ curl localhost:8080/?echo_body=amazing

"amazing"

Custom Body with environment variable value

➜ curl --header 'X-ECHO-ENV-BODY: HOSTNAME' localhost:8080
➜ curl localhost:8080/?echo_env_body=HOSTNAME

"c53a9ed79fa2"
for i in {1..10}do
➜    curl localhost:8080/?echo_env_body=HOSTNAME
➜ done

"c53a9ed79fa2"
"f10c3af61e40"
"c53a9ed79fa2"
"f10c3af61e40"
"c53a9ed79fa2"

Custom Headers

➜ curl --header 'X-ECHO-HEADER: One:1' localhost:8080
➜ curl localhost:8080/?echo_header=One:1

HTTP/1.1 200 OK
One: 1
➜ curl --header 'X-ECHO-HEADER: One:1, Two:2' localhost:8080
➜ curl "localhost:8080/?echo_header=One:1,%20Two:2"

HTTP/1.1 200 OK
One: 1
Two: 2

Custom response latency

➜ curl --header 'X-ECHO-TIME: 5000' localhost:8080
➜ curl "localhost:8080/?echo_time=5000"

⏳... 5000 ms

You can change default validations with

ENVIRONMENT CLI Default
CONTROLS__TIMES__MIN –controls:times:min 0
CONTROLS__TIMES__MAX –controls:times:max 60000

(Latency is defined in milliseconds)

File/Folder explorer

➜ curl --header 'X-ECHO-FILE: /' localhost:8080
➜ curl "localhost:8080/?echo_file=/"

["app", "bin", "etc", "usr", "var"]

Combine custom actions

➜ curl --header 'X-ECHO-CODE: 401' --header 'X-ECHO-BODY: Oups' localhost:8080
➜ curl "localhost:8080/?echo_body=Oups&echo_code=401"

HTTP/1.1 401 Unauthorized
"Oups"