Skip to content

Instantly share code, notes, and snippets.

@samsonjs
Forked from mranney/gzip_example.js
Created May 10, 2010 03:06
Show Gist options
  • Save samsonjs/395629 to your computer and use it in GitHub Desktop.
Save samsonjs/395629 to your computer and use it in GitHub Desktop.
var sys = require("sys"),
http = require("http"),
port = 12345,
spawn = require("child_process").spawn;
function gzipStr (str, callback) {
var child = spawn("gzip", ["-c", "-f", "-n"]),
stdout = "", stderr = "";
child.stdout.addListener("data", function (chunk) {
stdout += chunk;
});
child.stderr.addListener("data", function (chunk) {
stderr+= chunk;
});
child.addListener("exit", function (code) {
sys.puts("input: \""+ str+ "\"\n-->"+ hexdump(stdout));
callback(code ? "" : stdout);
});
child.stdout.setEncoding('binary');
child.stdin.write(str, "utf8");
child.stdin.close();
};
function hexdump (input, r, i) {
r= "";
if (typeof input === "string") {
i= 0;
while (i < input.length) {
r+= hexdump(input.charCodeAt(i++));
}
} else if (typeof input === "number") {
i= input.toString(16);
i= (i.length < 2) ? "0"+i : i;
r= i+ ".";
}
return r;
}
http.createServer(function (request, response) {
if (request.url.indexOf("favicon") >= 0) {
response.writeHeader(404, {});
response.write("");
return response.close();
}
gzipStr("this is a test", function (str) {
response.writeHeader(200, {
"Content-Type": "text/plain",
"server":"Node.js",
"Content-Encoding":"gzip",
"Connection":"close",
"Content-Length":str.length
});
response.write(str, "binary");
response.close();
});
}).listen(port);
sys.puts("Server running at http://localhost:"+ port+ "/");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment