Skip to content

Instantly share code, notes, and snippets.

@knbknb
Last active April 14, 2022 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knbknb/189724bcbc59859b53f7f024dac97a62 to your computer and use it in GitHub Desktop.
Save knbknb/189724bcbc59859b53f7f024dac97a62 to your computer and use it in GitHub Desktop.
some small node / javascript snippets with very basic tasks
// 2022 - does no longer work
// node: assume node-fetch is installed.
// alias node='export NODE_PATH=$NODE_PATH:$NVM_BIN/../lib/node_modules && node --use-strict'
// const fetch = require("node-fetch"); // load -g module
(async () => {const data = await fetch("http://api.figshare.com/v1/categories", {
"credentials": "include",
"headers": {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Upgrade-Insecure-Requests": "1",
"Cache-Control": "max-age=0"
},
"referrer": "https://hckrnews.com/",
"method": "GET",
"mode": "cors"
});
const jsonData = await data.json();
console.log(jsonData);
//process.stdout.write(JSON.stringify(jsonData));
})();
// read in a small textfile asynchronously
// with nodejs
// assumes that you are in your Linux home directory
// and a simple textfile file '.wgetrc' exists
var fs = require("fs");
var infile = __dirname + "/.wgetrc";
var fn = function(error, contents){
if(error){
console.log(error);
return;
}
console.log(contents)
}
fs.readFile(infile, "utf8", fn);
@knbknb
Copy link
Author

knbknb commented Dec 10, 2019

previous snippet, on error, returns

{ [Error: ENOENT: no such file or directory, open '..Xauthority']
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '..Xauthority' }

on success returns the file contents, and keeps the file open

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment