SHARE
TWEET

Untitled

a guest Sep 19th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs')
  2. const EventEmmiter = require('events')
  3.  
  4. class HandleFiles extends EventEmmiter {
  5.   readFile(path, opts) {
  6.     fs.readFile(path, opts, (err, data) => {
  7.       if (err) throw new Error(err)
  8.  
  9.       const content = Buffer.from(data, 'base64').toString()
  10.       this.emit('readFileDone',content)
  11.     })
  12.   }
  13.   writeFile (path, content) {
  14.     fs.writeFile(path, content, err => {
  15.       if (err) throw new Error(err)
  16.       this.emit('writeFileDone')
  17.     })
  18.   }
  19. }
  20.  
  21. const handleFiles = new HandleFiles();
  22.  
  23.  
  24. handleFiles.on('readFileDone', (content) => handleFiles.writeFile('./content.txt',content));
  25. handleFiles.on('writeFileDone', ()=>console.log('The file has been written correctly.'))
  26. handleFiles.readFile('base64.txt', 'utf-8')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top