Skip to content

Instantly share code, notes, and snippets.

View upning's full-sized avatar

Ning Chen upning

View GitHub Profile
@upning
upning / first-node.js
Created November 13, 2022 12:02
[NodeJS Hello World] My first introduction to server environment #nodejs
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
@upning
upning / eth_acct_gen.py
Created September 12, 2022 11:19
[Private Key Generator] Traders/bots on Ethereum use "fancy" addresses with leading/trailing 0s. (e.g., 0x00000a600e64c26be24f64169c89f21c1f530000). This can be easily accomplished with brute force. Try the python code we share below offline. Remember you should never share your private key with anyone! #crypto
============
# install web3py first
!pip install web3
from eth_account import Account
import secrets
acct = None
expect = "000"
while acct is None or acct.address[2:2+len(expect)] != expect:
@upning
upning / merge-file.py
Created August 17, 2022 08:10
[Merge File] Using python to merge multiple files into one #Util
import glob
read_files = glob.glob("*.md")
with open("new-file.md", "wb") as outfile:
for f in read_files:
with open(f, "rb") as infile:
outfile.write(infile.read())
@upning
upning / async-post-boilerplate.js
Last active April 19, 2022 07:46
[Async POST request] The boilerplate code to create a GET request with async and await
// Asynchronous functions
// Code goes here
const shortenUrl = async () => {
const urlToShorten = inputField.value;
const data = JSON.stringify({destination: urlToShorten});
try {
const response = await fetch('https://api-to-call.com/endpoint', {
method: 'POST',
body: data,
headers: {
@upning
upning / string-regex.js
Created April 4, 2022 15:58
[Remove space from a string] Using REGEX in JS #regex
/// https://stackoverflow.com/questions/5963182/how-to-remove-spaces-from-a-string-using-javascript
str = str.replace(/\s+/g, '');
// \s is the regex for "whitespace", and g is the "global" flag, meaning match ALL \s (whitespaces).
// A great explanation for + can be found https://stackoverflow.com/questions/5964373/is-there-a-difference-between-s-g-and-s-g
// Alternative
var a = b = " /var/www/site/Brand new document.docx ";
console.log( a.split(' ').join('') );
@upning
upning / object-keys.js
Created April 4, 2022 15:55
[Get keys on JS object or JSON] Modern JS has a special function for getting only the relevant keys out of an object
/// https://stackoverflow.com/questions/21076732/how-to-get-the-last-item-in-a-javascript-value-object-with-a-for-loop
// blind iteration
Object.keys(obj).forEach(function(key, i) {
var value = obj[key];
// do what you need to here, with index i as position information.
// Note that you cannot break out of this iteration, although you
// can of course use ".some()" rather than ".forEach()" for that.
});
@upning
upning / async-get-boilerplate.js
Last active April 4, 2022 15:26
[Async GET request] The boilerplate code to create a GET request with async and await
// async await GET
const getData = async () => {
try {
const response = await fetch('https://api-to-call.com/endpoint');
if (response.ok) {
const jsonResponse = await response.json();
// Code to execute with jsonResponse
}
throw new Error('Request failed!');
@upning
upning / fetch-post-boilerplate.js
Last active April 4, 2022 03:59
[POST request using fetch] The boilerplate code to create a GET request
// fetch POST
fetch('http://api-to-call.com/endpoint', {
method: 'POST',
headers: {
'Content-type': 'application/json',
'apikey': apiKey
}
body: JSON.stringify({id: '200'})
}).then(response => {
@upning
upning / fetch-get-boilerplate.js
Last active April 4, 2022 15:27
[GET request using Fetch] The boilerplate code to create a GET request
// fetch GET
fetch('http://api-to-call.com/endpoint').then(response => { // Sends request
if (response.ok) {
return response.json(); // converts response object to JSON
}
throw new Error('Request failed!');
}, networkError => console.log(networkError.message)
).then(jsonResponse => {
// Code to execute with jsonResponse
@upning
upning / backtick-string.js
Last active April 3, 2022 15:15
[Backtick ` as string literal] Using the backtick ` as the delimiter in ECMA6 to allow for multi-line strings and string interpolation with embedded expressions, and for special constructs called tagged templates.
// https://stackoverflow.com/questions/27678052/usage-of-the-backtick-character-in-javascript
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
let person = {name: 'RajiniKanth', age: 68, greeting: 'Thalaivaaaa!' };
// Usual HTML String
let usualHtmlStr = "<p>My name is " + person.name + ",</p>\n" +
"<p>I am " + person.age + " old</p>\n" +
"<strong>\"" + person.greeting + "\" is what I usually say</strong>";
// New HTML String