Skip to content

Instantly share code, notes, and snippets.

@anchnk
Last active April 27, 2017 13:55
Show Gist options
  • Save anchnk/74873396cf6424802be7077890f450cd to your computer and use it in GitHub Desktop.
Save anchnk/74873396cf6424802be7077890f450cd to your computer and use it in GitHub Desktop.
WHATWG file URL doc examples https://github.com/nodejs/node/pull/12670
'use strict';
const fs = require('fs');
const { URL } = require('url');
const hosts_file_url = new URL(`file://hostname/p/a/t/h/file.txt`);
fs.readFile(hosts_file_url, 'utf-8', (err,data) => {
if (err) throw err; // error is throw
console.log(data);
});
'use strict';
const fs = require('fs');
const { URL } = require('url');
const hosts_file_url = new URL(`file://hostname/p/a/t/h/file.txt`); // path convereted to UNC
fs.readFile(hosts_file_url, 'utf-8', (err,data) => {
if (err) throw err;
console.log(data); // read file's content
});
'use strict';
const fs = require('fs');
const { URL } = require('url');
const hosts_file_url = new URL('file:///etc/hosts');
fs.readFile(hosts_file_url, 'utf-8', (err,data) => {
if (err) throw err;
console.log(data);
});
'use strict';
const fs = require('fs');
const { URL } = require('url');
const hosts_file_url = new URL(`file:///${process.env['SystemRoot']}/System32/drivers/etc/hosts`);
fs.readFile(hosts_file_url, 'utf-8', (err,data) => {
if (err) throw err;
console.log(data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment