Added streams examples
This commit is contained in:
@@ -0,0 +1,56 @@
|
|||||||
|
/* @flow */
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
const url = require("url");
|
||||||
|
const querystring = require("querystring");
|
||||||
|
const http = require("http");
|
||||||
|
|
||||||
|
function processRequest(req, res, body) {
|
||||||
|
/*
|
||||||
|
Get parameters, both from the URL and the request body
|
||||||
|
*/
|
||||||
|
const urlObj = url.parse(req.url, true);
|
||||||
|
const urlParams = urlObj.query;
|
||||||
|
const bodyParams = querystring.parse(body);
|
||||||
|
|
||||||
|
console.log("URL OBJECT", urlObj);
|
||||||
|
console.log("URL PARAMETERS", urlParams);
|
||||||
|
console.log("BODY PARAMETERS", bodyParams);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Here you would analyze the URL to decide what is required
|
||||||
|
Then you would do whatever is needed to fulfill the request
|
||||||
|
Finally, when everything was ready, results would be sent
|
||||||
|
In our case, we just send a FINISHED message
|
||||||
|
*/
|
||||||
|
|
||||||
|
res.writeHead(200, "OK");
|
||||||
|
res.end(`FINISHED WITH THE ${req.method} REQUEST`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Minimal care for exceptions...
|
||||||
|
*/
|
||||||
|
process.on("uncaughtException", function(err) {
|
||||||
|
console.error("UNCAUGHT EXCEPTION...");
|
||||||
|
console.error(err);
|
||||||
|
console.error(err.stack);
|
||||||
|
});
|
||||||
|
|
||||||
|
http
|
||||||
|
.createServer((req, res) => {
|
||||||
|
// For PUT/POST methods, wait until the
|
||||||
|
// complete request body has been read.
|
||||||
|
if (req.method === "POST" || req.method === "PUT") {
|
||||||
|
let body = "";
|
||||||
|
|
||||||
|
req.on("data", data => {
|
||||||
|
body += data;
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on("end", () => processRequest(req, res, body));
|
||||||
|
} else {
|
||||||
|
return processRequest(req, res, "");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.listen(8080, "localhost");
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
/* @flow */
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
const zlib = require("zlib");
|
||||||
|
const fs = require("fs");
|
||||||
|
|
||||||
|
const inputStream = fs.createReadStream(
|
||||||
|
"/home/fkereki/Documents/CHURCHES - Digital Taxonomy.pdf"
|
||||||
|
);
|
||||||
|
|
||||||
|
const gzipStream = zlib.createGzip();
|
||||||
|
|
||||||
|
const outputStream = fs.createWriteStream(
|
||||||
|
"/home/fkereki/Documents/CHURCHES.gz"
|
||||||
|
);
|
||||||
|
|
||||||
|
inputStream.pipe(gzipStream).pipe(outputStream);
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
/* @flow */
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
const http = require("http");
|
||||||
|
const zlib = require("zlib");
|
||||||
|
const fs = require("fs");
|
||||||
|
|
||||||
|
http
|
||||||
|
.createServer(function(request, response) {
|
||||||
|
// Tell the client, this is a zip file.
|
||||||
|
response.writeHead(200, {
|
||||||
|
"Content-Type": "application/zip",
|
||||||
|
"Content-disposition": "attachment; filename=churches.gz"
|
||||||
|
});
|
||||||
|
|
||||||
|
const inputStream = fs.createReadStream(
|
||||||
|
"/home/fkereki/Documents/CHURCHES - Digital Taxonomy.pdf"
|
||||||
|
);
|
||||||
|
|
||||||
|
const gzipStream = zlib.createGzip();
|
||||||
|
|
||||||
|
inputStream.pipe(gzipStream).pipe(response);
|
||||||
|
})
|
||||||
|
.listen(8080, "localhost");
|
||||||
Reference in New Issue
Block a user