Created
April 7, 2022 12:47
-
-
Save Hegabovic/1c0fe74aba58c4ad97f2af1925391ffc to your computer and use it in GitHub Desktop.
Lab2 : NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Emitter } from "./constructorFunction.mjs" | |
let emit = new Emitter(); | |
emit.on("login",function(){ | |
console.log("hello you are logged in"); | |
}) | |
emit.emit("login") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function Emitter() { | |
this.events = {} | |
} | |
Emitter.prototype.on = function (type, listener) { | |
this.events[type] = this.events[type] || []; | |
this.events[type].push(listener) | |
} | |
Emitter.prototype.emit = function(type){ | |
if(this.events[type]){ | |
this.events[type].forEach(function (listener){ | |
listener() | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ================================================================ | |
* =========================== File Handle ======================== | |
* ================================================================ | |
* */ | |
import readLine from "readline"; | |
import fs from "fs"; | |
let fileHandle = readLine.createInterface({ | |
input: fs.createReadStream(new URL("greet.text",import.meta.url)), | |
output: process.stdout | |
}) | |
fileHandle.on("line",()=>{ | |
console.log("**************************************************"); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ================================================================ | |
* ===================== Files With NodeJS ======================== | |
* ================================================================ | |
* */ | |
/* | |
* ================================================================ | |
* ========================== Async Method ======================== | |
* ================================================================ | |
* */ | |
import { readFile,writeFile } from "fs/promises"; | |
let fileData = await readFile(new URL("data.json", import.meta.url), 'utf-8'); | |
console.log(fileData) | |
/* | |
* ================================================================ | |
* ========================== sync Method ========================= | |
* ================================================================ | |
* */ | |
import fs from "fs"; | |
fs.readFile(new URL("data.json", import.meta.url),'utf-8' ,(error,data) => { | |
if (error) { | |
console.error(error); | |
return error; | |
} else { | |
console.log(data); | |
// console.log(data.toString()) | |
} | |
}) | |
/* | |
* ================================================================ | |
* ========================== Rename File ========================= | |
* ================================================================ | |
* */ | |
fs.rename("test.txt","info.txt",()=>{ | |
console.log("file name changed!") | |
}) | |
/* | |
* ================================================================ | |
* ========================== remove file ========================= | |
* ================================================================ | |
* */ | |
fs.unlink("./test1.txt",()=>{ | |
console.log("file removed"); | |
}) | |
/* | |
* ================================================================ | |
* ========================= Write to file ======================== | |
* ================================================================ | |
* */ | |
await writeFile("info.txt","Hello iti (Hegabovic)","utf-8"); | |
// create a directory | |
fs.mkdir("./NewDirectory",null,(error)=>{ | |
if(error){ | |
console.error(error); | |
return error; | |
} else{ | |
console.log("directory has been created successfully"); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment