Create a gist now

Instantly share code, notes, and snippets.

"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