Skip to content

Commit

Permalink
readLine nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
S4nfs committed Apr 11, 2024
1 parent b58ea91 commit 3ff7850
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
18 changes: 8 additions & 10 deletions Node.JS_Course/10_Stream&Buffer.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
//Streams: Used to process(read and write) data piece by piece (chunks) therefore without keeping all the data in memory

const fs = require("fs");
const http = require("http");
const fs = require('fs')
const http = require('http')

const server = http.createServer();
const server = http.createServer()

server.on("request", (req, res) => {
server.on('request', (req, res) => {
// ==========================================================================================================
//1st Method Normal Way
// fs.readFile("Stream&Buffer.txt", (err, data) => {
// if (err) return console.error(err);
// res.end(data.toString());
// });


// ==========================================================================================================
//2nd Method Using Stream
//Handle stream events --> data, end, and error
Expand All @@ -28,11 +27,10 @@ server.on("request", (req, res) => {
// console.log(err);
// });


// ==========================================================================================================
//3rd Method Using pipe(realtime data) - Readble stream is much faster than sending the writeable reaponse to users thus forming "Backpressure" when response sent to data cannot be as fast as it is receiveing it from file, pipe fixes this problem by automatically leveraging the incoming and outgoing speed

// const rstream = fs.createReadStream("Stream&Buffer.txt");
// rstream.pipe(res);
});
server.listen(3000, "127.0.0.1");
const rstream = fs.createReadStream('Node.jS_Course/Stream&Buffer.txt')
rstream.pipe(res)
})
server.listen(3000, '127.0.0.1') //see http://localhost:3000 in browser
40 changes: 40 additions & 0 deletions Node.JS_Course/11_ReadLine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*ReadLine: Open a file and return the content line by line. Here is a small example of readline where i am using it to stream logs from my files line by line in order to send limited logs to the apollo subscriptions. See https://notepad.link/IycEM
some readline methods:
rl.close(): readline.InterfaceClose the instance. this is 'close'Raises an event and does not receive any further input.
rl.pause(): Pauses the input stream. this is 'pause'Raises an event.
rl.resume(): Resumes a paused input stream.
rl.question(query, callback): Ask the user a question, receive input, and pass it to the callback function.
rl.write(data[, key]): Write data to the terminal or simulate virtual keystrokes.
some readline event:
'line': Occurs whenever a new line enters the input stream. This event is mainly used when processing input data line by line.
'pause': Occurs when the input stream is paused.
'resume': Occurs when the input stream is restarted.
'close': rl.close()the method is called readline.InterfaceOccurs when the instance is closed.
*/
var readline = require('readline')
var fs = require('fs')

var rl = readline.createInterface({
input: fs.createReadStream('Node.jS_Course/ReadLineByLine.txt'),
})

let lineno = 0
rl.on('line', function (line) {
lineno++
console.log('Line number ' + lineno + ': ' + line)
})

// let readLine = readline.createInterface({
// input: process.stdin,
// output: process.stdout,
// })

// readLine.question('What is your name?', (answer) => {
// console.log(`Hello, ${answer}!`)

// readLine.close()
// })
5 changes: 5 additions & 0 deletions Node.JS_Course/ReadLineByLine.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This is line number one.
This is line number two.
This is line number three.
This is line number four.
This is line number five.

0 comments on commit 3ff7850

Please sign in to comment.