Skip to content

Instantly share code, notes, and snippets.

@Hegabovic
Created April 7, 2022 12:47
Show Gist options
  • Save Hegabovic/1c0fe74aba58c4ad97f2af1925391ffc to your computer and use it in GitHub Desktop.
Save Hegabovic/1c0fe74aba58c4ad97f2af1925391ffc to your computer and use it in GitHub Desktop.
Lab2 : NodeJS
import { Emitter } from "./constructorFunction.mjs"
let emit = new Emitter();
emit.on("login",function(){
console.log("hello you are logged in");
})
emit.emit("login")
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()
})
}
}
/*
* ================================================================
* =========================== 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("**************************************************");
})
/*
* ================================================================
* ===================== 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