Skip to content

Instantly share code, notes, and snippets.

View akirattii's full-sized avatar

Akira TANAKA akirattii

View GitHub Profile
@akirattii
akirattii / nginx-example.com.conf
Last active April 12, 2019 20:19
Typical example of nginx (v1.10.3) conf file for using ssl and collaboration with nodejs app
# nginx conf file example in the case of using:
# - nodejs app server
# - letsencrypt for SSL
# version: nginx/1.10.3
# /etc/nginx/conf.d/example.com.conf
# Permits for tester to access even if web server is under maintenance:
geo $allow_ip_flag {
default 0; # Not Allowed
@akirattii
akirattii / backup-restore-mysql-database.sh
Last active March 19, 2019 04:23
How to Backup and Restore the MySQL database or its tables.
# BACKUP
## Backup all tables of a specific database:
mysqldump -h {HOST} -P{PORT} -u{USER} -p{DB} > {DB}.dump.sql
## Backup a specific table:
mysqldump -h {HOST} -P{PORT} -u{USER} -p{DB} -t -c --skip-extended-insert {TABLE} > {DB}.{TABLE}.dump.sql
# RESTORE
## Restore all tables of a specific database from backuped file:
mysql -h {HOST} -P{PORT} -u{USER} -p{DB} < {DB}.dump.sql
## Restore a specific table from backuped file:
@akirattii
akirattii / mysqlで大文字小文字を区別する・しない.sql
Created March 3, 2017 23:31
MySQLで大文字と小文字を区別して検索する方法
-- MySQL
-- 大文字と小文字を区別しない検索:
select * from mytable where name like 'a'; -- <= "a", "A" どちらにもヒットする
-- 大文字と小文字を区別する検索:
select * from mytable where name like BINARY 'A'; -- <= likeの後に `BINARY` をつけることで "A" にだけヒットする
@akirattii
akirattii / stepAsyncFuncs.js
Last active March 8, 2017 07:35
How to execute async functions in order using pure js
// Executes async functions in order using pure js
/**
* Loop async func in order
* @param {Array} - array each object has below properties:
* - fn: {Function} - function to loop
* - param: {Object} - parameter to pass the function
* @param {Function} - error-first style callback returns below params as argument:
* - errs: array of error
* - results: array of result
@akirattii
akirattii / arraySuffle.js
Created March 9, 2017 03:42
Shuffles array using pure js
/**
* Shuffles array
* @param {Array} - array
*/
function shuffle(a) {
let len = a.length;
for (let i = len; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
@akirattii
akirattii / tweet-bot-using-nightmare.js
Created March 13, 2017 04:19
Tweetbot example using Nightmare
// TODO: Change your username/password:
const TW_USER = "<YOUR_USERNAME>";
const TW_PASS = "<YOUR_PASSWORD>";
var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });
nightmare
.goto('https://twitter.com/login/')
.insert('.js-username-field.email-input.js-initial-focus[name="session[username_or_email]"]', TW_USER)
@akirattii
akirattii / member-page-scraping.js
Last active March 15, 2017 04:24
How to scrape the member page that login required.
/**
$ node member-page-scraping.js <loginId> <password>
*/
var request = require("request");
request = request.defaults({ jar: true });
var encoding = require('encoding-japanese');
const cheerio = require("cheerio");
const Step = require("step");
var config = require("config");
@akirattii
akirattii / lineread-and-write.js
Created March 31, 2017 21:34
Read and write every line of input.txt into output.txt
/*
* Read and write every line of input.txt into output.txt
*/
const fs = require("fs");
var writeStream = fs.createWriteStream("output.txt");
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream("input.txt")
@akirattii
akirattii / deasync-example.js
Last active July 6, 2017 06:35
How to use an excellent module "deasync" which makes async function to sync function easily.
/**
NOTE: deasync can not use on browser.
*/
var deasync = require('deasync');
// any async function error-first callback styled
function asyncFn(p, cb) {
let res = "hello " + p;
let err = null;
return cb && cb(err, res);
@akirattii
akirattii / urlencoding-by-charset.js
Created April 18, 2017 03:03
文字コードを指定してURLエンコードするサンプル
// npm install encoding-japanese
const encoding = require('encoding-japanese');
urlEncode("餅", "EUCJP"); // "%CC%DF"
function urlEncode(str, charset) {
let eucArray = encoding.convert(Encoding.stringToCode(str), charset);
return encoding.urlEncode(eucArray);
}