Log.log(work)

いろんな作業メモ

DEPTH_ZERO_SELF_SIGNED_CERT

Node.js上でサンプルコードを書いていざ実行してみたら
DEPTH_ZERO_SELF_SIGNED_CERT
で怒られた。

しらべたらサーバへ接続する際に自前のオレオレ証明書が原因で認証エラーになっていたので
POSTの前におまじないを唱えたらうまく流れました。

// 証明書エラー回避
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

// リクエスト送信
request.post(
・・・


追記:
もしくは環境変数にセットできるならセットしたほうが楽かな。

export NODE_TLS_REJECT_UNAUTHORIZED=0

ソート、重複除く、などJavascript memo

  • 昇順
  array.sort( function(val1,val2){
    if(val1.propety1< val2.propety1) return -1;
    if(val2.propety1< val1.propety1) return 1;
    return 0
  });

それ以外はこちら
JavaScript つい忘れてしまう配列のソート方法 - Qiita

  • オブジェクトの中身拝見
console.log(JSON.stringify(arrayObj));
  • 重複除く
var hoge = foo.filter(function(x,i,self){return self.indexOf(x) ===i});
  • 同じキーがあるかどうか
Object.keys(dataObj).filter(function(k) { return dataObj[k] == searchword})[0]
// ない場合 === undefined
  • 金額のカンマ編集
1000.toString().replace(/(\d)(?=(\d{3})+$)/g , '$1,')
  • 今日の日付を書式yyyymmdd(月と日を必要に応じて0埋め)で取得する。
var now = new Date();
var yyyymmdd = now.getFullYear()+
	( "0"+( now.getMonth()+1 ) ).slice(-2)+
	( "0"+now.getDate() ).slice(-2);
var tomorrow = now.getFullYear()+
	( "0"+( now.getMonth()+1 ) ).slice(-2)+
	( "0"+( now.getDate()+1) ).slice(-2);
  • 配列→文字列

区切り文字列で結合

ary = ['hoge','foo','bar','piyo'];
str = ary.join(',');
console.log(str);
//=>hoge,foo,bar,piyo
  • 文字列→配列

区切り文字列で配列にする。

str = 'hoge,foo,bar,piyo';
ary = str.split(',');
console.log(ary);
//=>["hoge", "foo", "bar", "piyo"] 
  • 小数点の編集

・ 切捨て

Math.floor(val) 

・ 切り上げ

Math.ceil(val)

・ 四捨五入

Math.round(val)

MEANスタックの環境をつくる(途中)

OS: CentOS 6.7

Node.jsとnpmをinstall

sudo yum install epel-release
sudo yum install nodejs
node -v

バージョンでたらインストール完了

yum install -y npm --enablerepo=epel

npm も入れとく

Expressいれる

npm install express

MVSサイト構築

express コマンドを使ってサイトの雛形生成からサイトの動作確認まで行う

mkdir dev

cd dev

express myapp -e ejs

cd myapp 

npm install

npm start

サーバーが立ち上がったらローカルホスト:3000 で確認。


参考:
Node.js モジュール追加(Express) - Qiita

bootstrapをいれる

見た目よくするためにbowerを使ってインストール。
インストールしたら初期化しておく。

npm install -g bower

bower init

初期化の質問は下記参照:
Bowerまとめ(概要・導入・コマンド一覧) - Qiita


作業DIR上でbootstrapインストール ※依存関係にあるjqueryも同時に入る

bower install bootstrap --save


あとはMongoとAngular入れてひと段落。

sublime3 のPackage Controlのインストール

  • Proxyなしの場合
import urllib.request,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa1548d1514676163dafc88'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)
  • Proxy環境下の場合

http://user:pass@address:port のユーザー名やパスワードは各自設定。

import urllib.request,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa1548d1514676163dafc88'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler('http': 'http://user:pass@address:port')) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)

Gitでうっかり違うメアドでコミットしちゃった件

なんか知らない人がコミットしているというのがばれないように後で修正する。

rebaseの魔法を唱える

$ git rebase -i HEAD~2

エディタが開いて、行頭に pick がついたリストが出てくるので、
修正対象を「edit」に修正して:wq。

すると修正チャンス到来。

$ git rebase -i HEAD~2
Stopped at xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx... hoge
You can amend the commit now, with

        git commit --amend

Once you are satisfied with your changes, run

        git rebase --continue

メアドとユーザーを変更してコミット。

shinomiya@foo ~foo (branch|REBASE-i 2/2)
$ git commit --amend --author="user.name <user@example.com>"
[detached HEAD 0000000] hoge
 Date: Thu Jun 16 19:46:27 2016 +0900
 1 file changed, 18 insertions(+), 18 deletions(-)

あとはrebase --continue で残りのコミットを走らせる。

shinomiya@foo ~foo (branch|REBASE-i 2/2)
$ git rebase --continue
Successfully rebased and updated refs/heads/branch.