Friday, April 14, 2017

Node.js Setup

  • Advantages
    • High productivity by using the same language in both FE and BE
    • Easy to implement WebSocket using socket.io
    • Node.js uses non-blocking IO model; IO processing in 
  • Disadvantages
    • Single thread model:
      • If one job takes time, it impacts the overall system performance
      • Hard to optimize using CPU in multi core system; requires cluster 
  • Node.js Install
    • sudo dnf install nodejs
    • node -v
    • update:
      • sudo npm cache clean -f
      • sudo npm install -g n
      • sudo n stable
      • sudo n latest
      • sudo ln -sf /usr/local/n/versions/node/x.x.x/bin/node /usr/bin/node
      • node -v
    • sudo dnf install npm
    • sudo npm install npm@latest -g
    • npm -v
  • Express install
    • npm init
    • npm install express --save
    • npm install body-parser --save : to process json, etc
    • npm install cookie-parser --save
    • npm install multer --save : to process multi-post form
  • Swagger
    • npm install -g swagger
    • cd /usr/local/bin
    • ln -s /usr/local/n/versions/node/7.9.0/bin/swagger
    • example:
      • https://scotch.io/tutorials/speed-up-your-restful-api-development-in-node-js-with-swagger
    • file upload:
      • http://swagger.io/docs/specification/file-upload/

  • MongoDB
    • dnf install mongod mongodb-server mongo-tools
    • systemctl start/enable mongod
    • for specific versions:
    • vi /etc/yum.repos.d/mongodb.repo
         [mongodb-org-2.6]
         name=MongoDB Repository
         baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
         gpgcheck=0
         enabled=1

    [mongodb-org-3.4]
    name=MongoDB Repository
 baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
    gpgcheck=1
    enabled=1
 gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
    • sudo dnf install mongodb-org mongodb-org-server
    • sudo systemctl start/enable mongod
    • mongostat or mongostat --rowcount 5 2
    • command line: mongo
    • config: /etc/mongod.conf
    • npm install mongoose --save
  • Rock mongo
    • sudo yum install httpd gcc php php-gd php-pear php-devel openssl-devel unzip wget
    • If you use Fedora 25, you need to downgrade PHP 7.0 to PHP 5.6 before going to the next steps:
      • dnf remove php
      • wget https://rpms.remirepo.net/fedora/remi-release-25.rpm
      • rpm -Uvh remi-release-25.rpm
      • dnf --enablerepo=remi install php56 php56-php-common php56-php-cli php56-php-mbstring php56-php-mcrypt php56-php-pear php56-php-xml php56-runtime php56-php-mysqlnd php56-php-devel php56-php-fpm
      • module load php56
      • systemctl restart httpd
      • cd /etc
      • ln -s /opt/remi/php56/root/etc/php.ini
    • sudo pecl install mongo
    • sudo vi /etc/php.ini
    • extension=mongo.so
    • sudo systemctl restart httpd
    • php -m | grep -i mongo
    • If you use Fedora 25, you need to downgrade PHP 7.0 to PHP 5.6 before going to the next steps:
      • systemctl enable php56-php-fpm
      • systemctl start php56-php-fpm
    • wget https://github.com/iwind/rockmongo/archive/master.zip
    • unzip master.zip
    • sudo mv rockmongo-master/ /var/www/html/rockmongo
    • sudo systemctl restart httpd
    • Unable to connect MongoDB, please check your configurations. MongoDB said:Failed to connect to: 127.0.0.1:27017: Permission denied.
      • /usr/sbin/setsebool -P httpd_can_network_connect 1
      • service httpd restart
  • PM2
    • npm install pm2 -g
    • pm2 start app.js
    • pm2 monit
    • pm2 list
    • pm2 stop
    • pm2 restart
    • pm2 delete
    • pm2 logs
  • Jade
    • http://html2jade.aaron-powell.com/