Skip to content

Instantly share code, notes, and snippets.

@codebubb
codebubb / clipboard.js
Created September 6, 2019 12:19
Copy to clipboard in JavaScript
<input id="inputText" value="Text to copy">
<button id="copyInputText">Copy</button>
<span id="elementText">More Text to copy</span>
<button id="copyElementText">Copy</button>
<script>
const execCopy = () => {
inputText.select();
document.execCommand('copy')
@codebubb
codebubb / echoserver.js
Last active September 9, 2019 10:36
Echo Server
// Simple Node.js Echo Server that listens for data and sends a response with the data back to the client
// Note: Responds to any verb eg. GET/POST etc.
const createServer = require('http').createServer;
const server = createServer((req, res) => {
let body = '';
req.on('data', data => body += data)
req.on('end', () => {
@codebubb
codebubb / select-example.js
Last active September 5, 2019 16:31
Changing field names in mongodb
// [{_id:"someid", name:"someUserName", age:21, countPosts: 15}];
db.findOne({ _id })
.then(results => ({ id: results._id, username: results.name }))
.then(results => res.json(results));
@codebubb
codebubb / decodesearchparams.js
Created September 2, 2019 12:54
Decode URL Search Params (to object)
const searchParams = new URLSearchParams(window.location.search);
const decodedParams = Array
.from(searchParams.keys())
.reduce((acc, key) => ({ ...acc, [key]: searchParams.get(key) }), {});
@codebubb
codebubb / ipaddress.js
Created September 2, 2019 12:40
How to get IP address with JavasScript
fetch('https://api.ipify.org/?format=json')
.then(results => results.json())
.then(console.log) // e.g. { ip: "113.114.194.164" }
.catch(console.error);
@codebubb
codebubb / submitform.html
Created August 21, 2019 12:54
Quick example of how to submit a form with JavaScript.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Submit a form with JavaScript</title>
<style>
html, body {
height: 100%;
@codebubb
codebubb / decodecookie.js
Last active December 18, 2022 13:41
Decode Cookie string to a single object
document.cookie
.split(';')
.map(cookie => cookie.split('='))
.reduce((accumulator, [key, value]) => ({ ...accumulator, [key.trim()]: decodeURIComponent(value) }), {});
// app.js
const MongoClient = require('mongodb').MongoClient;
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const ObjectID = require('mongodb').ObjectID;
MongoClient.connect('mongodb://localhost:27017/test', (err, client) => {
if (err) throw err;
const db = client.db('test');
const users = db.collection('users');
@codebubb
codebubb / mongodbrest.js
Created July 3, 2019 16:05
MongoDB RestFul calls
router.get('/', (req, res, next) => {
const users = req.app.locals.users;
users
.find({})
.toArray()
.then(data => res.json(data));
});
router.post('/', (req,res, next) => {
const users = req.app.locals.users;
@codebubb
codebubb / nodemod.js
Created June 4, 2019 14:46
node mod collection
const axios = require('axios');
const chalk = require('chalk');
const commander = require('commander');
commander
.version('1.0.0')
.option('-t --type <type>', 'Lookup type')
.option('-n --number <number>', 'Number of results')
.parse(process.argv);