mirror of
https://github.com/kevin-DL/complete-node-bootcamp.git
synced 2026-01-12 11:25:13 +00:00
Initial commit 🚀
This commit is contained in:
33
2-how-node-works/final/streams.js
Normal file
33
2-how-node-works/final/streams.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const fs = require("fs");
|
||||
const server = require("http").createServer();
|
||||
|
||||
server.on("request", (req, res) => {
|
||||
// Solution 1
|
||||
// fs.readFile("test-file.txt", (err, data) => {
|
||||
// if (err) console.log(err);
|
||||
// res.end(data);
|
||||
// });
|
||||
|
||||
// Solution 2: Streams
|
||||
// const readable = fs.createReadStream("test-file.txt");
|
||||
// readable.on("data", chunk => {
|
||||
// res.write(chunk);
|
||||
// });
|
||||
// readable.on("end", () => {
|
||||
// res.end();
|
||||
// });
|
||||
// readable.on("error", err => {
|
||||
// console.log(err);
|
||||
// res.statusCode = 500;
|
||||
// res.end("File not found!");
|
||||
// });
|
||||
|
||||
// Solution 3
|
||||
const readable = fs.createReadStream("test-file.txt");
|
||||
readable.pipe(res);
|
||||
// readableSource.pipe(writeableDest)
|
||||
});
|
||||
|
||||
server.listen(8000, "127.0.0.1", () => {
|
||||
console.log("Listening...");
|
||||
});
|
||||
Reference in New Issue
Block a user