Skip to content

Instantly share code, notes, and snippets.

@pmuellr
Created September 30, 2015 20:00
Show Gist options
  • Save pmuellr/b55948a7eb3d29b3ef6a to your computer and use it in GitHub Desktop.
Save pmuellr/b55948a7eb3d29b3ef6a to your computer and use it in GitHub Desktop.
"use strict"
const fs = require("fs")
const http = require("http")
let Port = process.env.PORT | "0"
let CountBlock = 100
let Requests = 0
let Responses = 0
const Server = http.createServer(onRequest)
Server.listen(Port, onListen)
setInterval(onInterval, 10)
//-------------------------------------
function onInterval() {
const url = `http://localhost:${Port}`
const req = http.get(url, res => onResponse(res))
req.on("error",
err => console.log("request error:", err.stack)
)
}
//-------------------------------------
function onResponse(res) {
Responses++
if (0 == Responses % CountBlock)
console.log(`recv: ${Responses} requests`)
}
//-------------------------------------
function onRequest(req, res) {
fs.readFile(__filename, "utf8",
(err, data) => onReadDone(res, err, data)
)
}
//-------------------------------------
function onReadDone(res, err, data) {
if (err) {
console.log("read error:", err.stack)
data = `${err}`
}
res.end(data)
Requests++
if (0 == Requests % CountBlock)
console.log(`send: ${Requests} requests`)
}
//-------------------------------------
function onListen() {
Port = Server.address().port
console.log(`server listing at http://localhost:${Port}`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment