Last active
July 12, 2019 01:24
-
-
Save jerryvig/4f3a91ffa3f9ccbdbd83bb6d99ec8bc3 to your computer and use it in GitHub Desktop.
TypeScript import node "fs" module
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 * as fs from 'fs'; | |
import * as https from 'https'; | |
function afterWrite(err): void { | |
if (err) { | |
throw new Error('Failed to write to file.'); | |
} | |
} | |
function writeSomeData(err, fd): void { | |
if (err) { | |
throw new Error('Failed to open file for writing'); | |
} else { | |
console.log('the file opened'); | |
fs.write(fd, 'some data to write', afterWrite); | |
} | |
} | |
function afterUnlink(err): void { | |
if (err) { | |
throw new Error('Failed to unlink the file'); | |
} else { | |
fs.open('./testfile', 'w', writeSomeData); | |
} | |
} | |
function openOrUnlink(err, stats): void { | |
if (err && !stats) { | |
fs.open('./testfile', 'w', writeSomeData); | |
} else if (err) { | |
throw new Error('Stats exist but error raised.'); | |
} else { | |
console.log('file exists unlinking....'); | |
fs.unlink('./testfile', afterUnlink); | |
} | |
} | |
fs.stat('./testfile', openOrUnlink); | |
https.get('https://www.pizzapatron.com', (message) => { | |
console.log('httpVersion = ' + message.httpVersion); | |
let buffer = ''; | |
message.on('data', (chunk) => { | |
buffer += chunk; | |
}); | |
message.on('end', () => { | |
console.log('data = ' + buffer); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment