Last active
April 14, 2022 15:34
-
-
Save knbknb/189724bcbc59859b53f7f024dac97a62 to your computer and use it in GitHub Desktop.
some small node / javascript snippets with very basic tasks
This file contains hidden or 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
// 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)); | |
})(); |
This file contains hidden or 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
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
previous snippet, on error, returns
on success returns the file contents, and keeps the file open