Skip to content

nvpmai/nodejs_prework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Proxy Server

This is a Proxy Server for Node.js submitted as the pre-work requirement for CoderSchool.

Time spent: 20 hours

Completed:

  • Required: Requests to port 8000 are echoed back with the same HTTP headers and body
  • Required: Requests/reponses are proxied to/from the destination server
  • Required: The destination server is configurable via the --host, --port or --url arguments
  • Required: The destination server is configurable via the x-destination-url header
  • Required: Client requests and respones are printed to stdout
  • Required: The --logfile argument outputs all logs to the file specified instead of stdout
  • Optional: The --exec argument proxies stdin/stdout to/from the destination program
  • Optional: The --loglevel argument sets the logging chattiness
  • Optional: Supports HTTPS
  • Optional: -h argument prints CLI API

Walkthrough Gif:

Echo and Proxy Server

nodemon index.js
curl -v http://127.0.0.1:8000 -d "hello self" -H "x-wg"f: yodaw
curl -v http://127.0.0.1:9000 -d "hello self" -H "x-wg"f: yodaw

echo and proxy server

--host; --url

nodemon index.js --url http://kenh14.vn/
curl http://127.0.0.1:9000 -d "Hi kenh14"

nodemon index.js --host google.com
curl http://127.0.0.1:9000 -d "Hi google"

--host; --url

--exec; --h; x-destination-url

nodemon index.js --h
cat index.js | nodemon index.js --exec "grep require"


#### --exec; --h; x-destination-url
```bash
nodemon index.js --h
cat index.js | nodemon index.js --exec "grep require"

nodemon index.js
curl http://127.0.0.1:9000 -H 'x-destination-url:http://webtretho.com'

--exec; --h; x-destination-url

--logfile; --loglevel

nodemon index.js --logfile log.txt --loglevel DEBUG
curl http://127.0.0.1:8000 -d "Hi Google"

nodemon index.js --logfile log.txt --loglevel INFORMATIONAL
curl http://127.0.0.1:8000 -d "Hi Google"

--logfile; --loglevel

--url-ssl

Make proxy Server support https

nodemon index.js --url-ssl http://127.0.0.1:8000
curl -k https://127.0.0.1:9000 -v

--url-ssl

Starting the Server

npm start

Features

Echo Server:

curl -v -X POST http://127.0.0.1:8000 -d "hello self" -H "x-asdf: yodawg"
* Rebuilt URL to: http://127.0.0.1:8000/
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> POST / HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/7.43.0
> Accept: */*
> x-asdf: yodawg
> Content-Length: 10
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 10 out of 10 bytes
< HTTP/1.1 200 OK
< host: 127.0.0.1:8000
< user-agent: curl/7.43.0
< accept: */*
< x-asdf: yodawg
< content-length: 10
< content-type: application/x-www-form-urlencoded
< Date: Wed, 21 Sep 2016 00:39:30 GMT
< Connection: keep-alive
<
* Connection #0 to host 127.0.0.1 left intact
hello self

Proxy Server:

Port 9000 will proxy to the echo server on port 8000.

curl -v http://127.0.0.1:9000/asdf -d "hello proxy"
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 9000 (#0)
> POST /asdf HTTP/1.1
> Host: 127.0.0.1:9000
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Length: 11
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 11 out of 11 bytes
< HTTP/1.1 200 OK
< host: 127.0.0.1:9000
< user-agent: curl/7.43.0
< accept: */*
< content-length: 11
< content-type: application/x-www-form-urlencoded
< connection: close
< date: Wed, 21 Sep 2016 00:41:04 GMT
<
* Closing connection 0
hello proxy

Configuration:

CLI Arguments:

The following CLI arguments are supported:

--host

The host of the destination server. Defaults to 127.0.0.1.

--port

The port of the destination server. Defaults to 80 or 8000 when a host is not specified.

--url

A single url that overrides the above. E.g., http://www.google.com

--url_ssl

Specify a forwarding http(s) url. Make proxy server support https

--logfile

Specify a file path to redirect logging to.

--loglevel

Specify the level to which only output logs greater than or equal to is recorded. ALERT: 0, ERROR: 1, WARNING: 2, INFORMATIONAL: 3, DEBUG: 4

--h

List all possible arguments for the server.

--exec

Specify stdin/stdout to/from the destination program.

Headers

The follow http header(s) are supported:

x-destination-url

Specify the destination url on a per request basis. Overrides and follows the same format as the --url argument.

References

https://github.com/linghuaj/node-proxy-server

http://stackoverflow.com/questions/34835859/node-js-https-example-error-unknown-ssl-protocol-error-in-connection-to-localh

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published