Skip to content

Instantly share code, notes, and snippets.

View midnightcodr's full-sized avatar

Rico Chen midnightcodr

View GitHub Profile
@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)
})
}
@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 / 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 / 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)

{
// 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 / 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')
}
@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 / README.md
Created December 9, 2018 03:42
test-mongo-db-cluster
start_cluster
docker exec -it rc1 mongo
# then run the following in container rc1's mongo shell
rs.initiate(config = { _id: 'test-set', members: [{ _id: 0, host: 'rc1:27017' }, { _id: 1, host: 'rc2:27017' }, { _id: 2, host: 'rc3:27017' }] })
# while in there, populate some test data
use test
db.list.insertOne({ somekey: 'some value' })
db.list.insertOne({ title: 'cool' })
# finally quit
@midnightcodr
midnightcodr / nsq-consumer.js
Created December 13, 2018 13:36
updated example based on original nsqjs github example
const nsq = require('nsqjs')
const reader = new nsq.Reader('sample_topic', 'test_channel', {
nsqdTCPAddresses: ['127.0.0.1:4150']
})
reader.connect()
reader.on('message', msg => {
console.log('Received message [%s]: %s', msg.id, msg.body.toString())
@midnightcodr
midnightcodr / app.js
Created March 29, 2019 02:39
hapi-with-mongoose-example
const Hapi = require('hapi')
const Joi = require('joi')
const Mongoose = require('mongoose')
const Boom = require('boom')
const server = Hapi.Server({
port: 3000,
routes: {
validate: {
failAction: (request, h, err) => {