Skip to content

Instantly share code, notes, and snippets.

View midnightcodr's full-sized avatar
💭
A

Rico Chen midnightcodr

💭
A
View GitHub Profile
@midnightcodr
midnightcodr / create-parcel-app.sh
Created November 7, 2018 04:27
create-parcel-app
create-parcel-app() {
[ $# -lt 1 ] && echo "$funcstack[1] project-name"
project=$1
if [ -e $project ]; then
echo "object $project already exists"
return
fi
mkdir -p $project/src
cd $project
cat << EOL > src/index.html
@midnightcodr
midnightcodr / example.js
Created August 23, 2018 16:28
output-svg-in-hapijs-17
handler: (request, h) => {
const captcha = svgCaptcha.create({
size: 5
})
return h.response(captcha.data).header('Content-Type', 'image/svg+xml')
}
// handy function to strip the plusing part of a email address with +
const stripPlusing = em => em.replace(/\+[^@]+/, '')
console.log(stripPlusing('myemailaddress@mycompany.com'))
console.log(stripPlusing('myemailaddress+123@mycompany.org'))
console.log(stripPlusing('myemailaddress+abc123@mycompany.net'))
/*
myemailaddress@mycompany.com
myemailaddress@mycompany.org
@midnightcodr
midnightcodr / readme.md
Last active July 25, 2018 20:22
Visual Studio Code settings for using standardjs

Extensions

ext install prettier-vscode
ext install prettier-standard-vscode
ext install vscode-standardjs

User Settings (CMD + , to activate)

{
@midnightcodr
midnightcodr / moduleA.js
Created June 28, 2018 16:30
using multiple authentication strategies in hapijs
const register = async server => {
server.route([
{
path: '/moduleA/info',
method: 'GET',
handler: request => {
return 'this is from moduleA/info'
}
}
])
@midnightcodr
midnightcodr / main.js
Created December 9, 2017 18:06
node-fetch with retry
const fetch = require('node-fetch')
const delay = (ms) => {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, ms)
})
}
const retryFetch = (url, fetchOptions={}, retries=3, retryDelay=1000) => {
@midnightcodr
midnightcodr / example.js
Last active November 27, 2017 22:03
hapijs 17 server.method async issue
const Hapi = require('hapi')
const double = (input) => {
return new Promise(resolve => {
setTimeout(() => {
return resolve(input+input)
}, 500)
})
}
// using array
const bluebird = require('bluebird')
const things = [{a: 1}, {b: 2}, {c: 3}]
function doit(doc) {
return bluebird.delay(1000).then(() => {return doc})
}
async function main() {
@midnightcodr
midnightcodr / test-hapi.js
Last active November 1, 2017 16:56
server.emit is not a function
async function main() {
const Hapi = require('./lib');
const server = Hapi.server({ port: 80 });
server.event('test');
server.events.once('test', (update) => console.log(update));
await server.emit('test', 'hello');
await server.emit('test', 'hello'); // Ignored
}
@midnightcodr
midnightcodr / User.js
Created July 2, 2017 05:04
using mongoose 4.11.0
const mongoose = require('mongoose')
mongoose.Promise = require('bluebird') // optional, use this to get rid of
// the mpromise DeprecationWarning
const conn = mongoose.createConnection('mongodb://localhost/testDB')
const Schema = mongoose.Schema
const UserSchema = new Schema({
username: String,
email: String
})