Skip to content

Instantly share code, notes, and snippets.

View bryanthuan's full-sized avatar
🏠
Working from home

Bryan Nguyen bryanthuan

🏠
Working from home
  • StorHub Group
  • Ho Chi Minh, Vietnam
View GitHub Profile
@bryanthuan
bryanthuan / bling.js
Created September 6, 2017 09:27 — forked from paulirish/bling.js
bling dot js
/* bling.js */
window.$ = document.querySelectorAll.bind(document);
Node.prototype.on = window.on = function (name, fn) {
this.addEventListener(name, fn);
}
NodeList.prototype.__proto__ = Array.prototype;
@bryanthuan
bryanthuan / Update remote repo
Created September 28, 2017 04:22 — forked from mandiwise/Update remote repo
Transfer repo from Bitbucket to Github
// Reference: http://www.blackdogfoundry.com/blog/moving-repository-from-bitbucket-to-github/
// See also: http://www.paulund.co.uk/change-url-of-git-repository
$ cd $HOME/Code/repo-directory
$ git remote rename origin bitbucket
$ git remote add origin https://github.com/mandiwise/awesome-new-repo.git
$ git push origin master
$ git remote rm bitbucket
// my-service.hooks.js
module.exports = {
before:{
find:[
async findQuery(context){
// get the client (Express global)
const { db } = context.app.get('knexClient')
// build and run the query
const q = db.raw(`SELECT * FROM TABLE`)
const res = await q
@bryanthuan
bryanthuan / knex-pagination.js
Created November 23, 2018 03:56 — forked from pirey/knex-pagination.js
Adding pagination to knex.js
const config = require('./config')
const knex = require('knex')(config.db)
const QueryBuilder = require('knex/lib/query/builder')
QueryBuilder.prototype.paginate = function ({ limit = 10, page = 1 }) {
const offset = (page - 1) * limit
return Promise.all([
this.clone().count('* as count').first(),
this.offset(offset).limit(limit)
@bryanthuan
bryanthuan / download-file.js
Created February 26, 2019 09:17 — forked from javilobo8/download-file.js
Download files with AJAX (axios)
axios({
url: 'http://localhost:5000/static/example.pdf',
method: 'GET',
responseType: 'blob', // important
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'file.pdf');
document.body.appendChild(link);
@bryanthuan
bryanthuan / nginx-tuning.md
Created April 14, 2019 06:04 — forked from denji/nginx-tuning.md
NGINX tuning for best performance

Moved to git repository: https://github.com/denji/nginx-tuning

NGINX Tuning For Best Performance

For this configuration you can use web server you like, i decided, because i work mostly with it to use nginx.

Generally, properly configured nginx can handle up to 400K to 500K requests per second (clustered), most what i saw is 50K to 80K (non-clustered) requests per second and 30% CPU load, course, this was 2 x Intel Xeon with HyperThreading enabled, but it can work without problem on slower machines.

You must understand that this config is used in testing environment and not in production so you will need to find a way to implement most of those features best possible for your servers.

@bryanthuan
bryanthuan / 0. nginx_setup.sh
Created April 15, 2019 12:13 — forked from mikhailov/0. nginx_setup.sh
NGINX+SPDY with Unicorn. True Zero-Downtime unless migrations. Best practices.
# Nginx+Unicorn best-practices congifuration guide. Heartbleed fixed.
# We use latest stable nginx with fresh **openssl**, **zlib** and **pcre** dependencies.
# Some extra handy modules to use: --with-http_stub_status_module --with-http_gzip_static_module
#
# Deployment structure
#
# SERVER:
# /etc/init.d/nginx (1. nginx)
# /home/app/public_html/app_production/current (Capistrano directory)
#
@bryanthuan
bryanthuan / js-nl2br-br2nl.md
Created April 18, 2019 10:03 — forked from yidas/js-nl2br-br2nl.md
JavaScript nl2br & br2nl functions

JavaScript nl2br & br2nl functions

The exchange of new line & br HTML tag could refer to PHP - nl2br() function, which uses to inserts HTML line breaks before all newlines in a string.

These JavaScript functions consider whether to use insert or replace to handle the swap.

nl2br

@bryanthuan
bryanthuan / nl2p.php
Created April 18, 2019 10:07
Simple nl2p (wraps things in paragraph tags as opposed to line breaks - nl2br)
<?php
/**
* This wraps blocks of text (delimited by \n) in p tags (similar to nl2br)
* @author Scott Dover <sdover102@gmail.com>
* @param str
* @return str
*/
function nl2p($string) {
/* Explode based on new-line */
@bryanthuan
bryanthuan / knex-paginator.js
Created April 27, 2019 17:26 — forked from ionutmilica/knex-paginator.js
Simple paginator function for knex
module.exports = (knex) => {
return async (query, options) => {
const perPage = options.perPage || 10;
let page = options.page || 1;
const countQuery = knex.count('* as total').from(query.clone().as('inner'));
if (page < 1) {
page = 1;
}