Skip to content

Instantly share code, notes, and snippets.

View midnightcodr's full-sized avatar

Rico Chen midnightcodr

View GitHub Profile
@midnightcodr
midnightcodr / sample_contact_us_form_bootstrap_styling.html
Created May 21, 2012 03:36
A sample contact form using twitter bootstrap styling
<form class="well span8">
<div class="row">
<div class="span3">
<label>First Name</label>
<input type="text" class="span3" placeholder="Your First Name">
<label>Last Name</label>
<input type="text" class="span3" placeholder="Your Last Name">
<label>Email Address</label>
<div class="input-prepend">
<span class="add-on"><i class="icon-envelope"></i></span><input type="text" id="inputIcon" class="span2" style="width:233px" placeholder="Your email address">
@midnightcodr
midnightcodr / README.md
Last active August 15, 2023 20:30
websocket performance test using ws
npm install bluebird ws
node server.js
node client.js
@midnightcodr
midnightcodr / bulk.js
Last active March 24, 2017 01:20
mongodb bulk insert with bluebird disposer
module.exports = function(col, limit=5000) {
this.bulk = col.initializeUnorderedBulkOp()
let cnt = 0
this.add = record => {
cnt++
this.bulk.insert(record)
if(cnt >= limit) {
this.flush()
}
@midnightcodr
midnightcodr / main.js
Created March 27, 2017 00:27
light switch with relay and raspberry pi
var gpio = require('rpi-gpio-mod');
function onoff(v) {
gpio.setup(8, gpio.DIR_OUT, write);
function write() {
gpio.write(8, v, function(err) {
if (err) throw err;
console.log('Written to pin');
@midnightcodr
midnightcodr / connect.js
Last active April 16, 2017 19:16
Bluebird mongodb disposer
// tested with mongodb 2.2.25
const MongoClient=require('mongodb').MongoClient
const Promise = require('bluebird')
module.exports = (connOpts) => {
return MongoClient
.connect(connOpts, {promiseLibrary: Promise})
// .then(db => db) /* not necessary */
.disposer(db => {
console.log('to disconnect')
@midnightcodr
midnightcodr / main.js
Created April 11, 2017 02:20
inquirer with async/await
const inquirer = require('inquirer')
const genList = (list) => {
const choices = list.map((item, index) => {
return {
key: index,
name: `${item.id}: ${item.quantity}@${item.price}`,
value: item.id
}
})
@midnightcodr
midnightcodr / github-issue-with-bluebird.js
Created April 11, 2017 14:02
github-issue-with-bluebird.js
const fetch = require('node-fetch')
const Promise = require('bluebird')
const headers = {
'User-Agent': 'YOUR-GITHUB-USERNAME'
}
const repos = [
'scottwrobinson/camo',
'facebook/react',
@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
})
@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
}
// 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() {