顯示具有 NodeJS 標籤的文章。 顯示所有文章
顯示具有 NodeJS 標籤的文章。 顯示所有文章

2015年11月15日

Ionic Framework 製作 Hybrid App - 1.準備環境

blog.ionic.io

因為一些大學課程需要的關係製作了這一系列的教學,在你深入的看這個教學之前,我們希望你可以先上網了解 Native App, Web App, Hybrid App 等等開發方法的優點和缺點,選擇最適合你的 App 開發方式。

你至少需要具備一些基礎的  HTML 和 Javascript 的知識來跟著我們實作這一系列的教學。


準備環境


我們需要 Node.js, npm, Cordova, and Ionic

什麼是 Node.js 和 npm?


Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

more: https://nodejs.org/en/

什麼是 Cordova?


Mobile apps with HTML, CSS & JS. Target multiple platforms with one code base. Free and open source.

more: https://cordova.apache.org/

https://blog.codecentric.de/en/2014/11/ionic-angularjs-framework-on-the-rise/


什麼是 Ionic Framework?

Create mobile apps with the web technologies you love. Free and open source, Ionic offers a library of mobile-optimized HTML, CSS and JS components, gestures, and tools for building highly interactive apps. Built with Sass and optimized for AngularJS.

more: http://ionicframework.com/


前往第二章 建立一個測試 App


2013年12月30日

nodejs simple server http-server

npm install http-server -g
http-server [path] [options]

options:
  -p                 Port to use [8080]
  -a                 Address to use [0.0.0.0]
  -d                 Show directory listings [true]
  -i                 Display autoIndex [true]
  -e --ext           Default file extension if none supplied [none]
  -s --silent        Suppress log messages from output
  --cors             Enable CORS via the 'Access-Control-Allow-Origin' header
  -o                 Open browser window after staring the server
  -c                 Set cache time (in seconds). e.g. -c10 for 10 seconds.
                     To disable caching, use -c-1.
  -h --help          Print this list and exit.

2013年11月27日

Keep MongoDB connection in nodejs 筆記

因為是小 project 使用 native mongodb nodejs driver
首先,使用新的 MongoClient 取代 Db
雖然範例裡面都有乖乖 db.close() ,但是其實不需要這樣,
更好的方法是持續這個 mongodb connection ,並且讓需要 mongodb 的部分共用同一個 connection。
一開始我的寫法類似範例裡的方式
mongo = require '\mongodb' mongoClient = mongodb.MongoClient mongodbURI = 'mongodb://127.0.0.1:27017/test' route.get '/abc', (req, res) -> mongoClient.connect mongodbURI, (err, db) -> throw err if err db.collection('abc').insert({foo:'bar'}, (err, result) -> throw err if err console.log "succes insert abc" db.close() ) 這樣的配置 nginx - nodejs - mongodb 在第二天大量流量進來的時候會噴發
nginx: connect() failed (111: Connection refused) while connecting to upstream [Error: failed to connect to [localhost:27017]] 這個跟 localhost 或是 127.0.0.1 無關
由於 process 結束會自動斷掉 connection ,以下嘗試拿掉 db.close()
route.get '/abc', (req, res) -> mongoClient.connect mongodbURI, (err, db) -> throw err if err db.collection('abc').insert({foo:'bar'}, (err, result) -> throw err if err console.log "succes insert abc" ) 這樣 mongodb 會自動處理 mongo connection ,但是量大的時候依然會噴發 [initandlisten] connection accepted from 127.0.0.1:41822 #277824 (7998 connections now open) 失去 nginx - nodejs 可以處理 C10K problem 的優勢
嘗試改成一開始就建立 mongodb connection 並且不關閉,
使用mongodb 預設 maxPoolSize=5 mongoClient.connect mongodbURI, (err, db) -> throw err if err route.get '/abc', (req, res) -> db.collection('abc').insert({foo:'bar'}, (err, result) -> throw err if err console.log "succes insert abc" ) route.get '/def', (req, res) -> db.collection('def').insert({foo:'bar'}, (err, result) -> throw err if err console.log "succes insert def" ) 以上處理上千個併發沒問題, mongodb 會自動處理 connection pool,只需要依需求調整 maxPoolSize
實際上可以把 mongodb 提出來
mongo.coffee mongodb = require 'mongodb' mongoClient = mongodb.MongoClient mongodbURI = 'mongodb://127.0.0.1:27017/test' module.exports.init = (callback) -> mongoClient.connect mongodbURI, (err, db) -> console.log err if err console.log 'opened mongodb connection' callback(err, db)
server.coffee mongo.init (err, db) -> throw err if err ...
順代一提,Apache Benchmark ab test 連線數量多的時候連線數量並不準確,
像是 -n 1000 -c 10 可能實際上送了 1007 次 request 之類的




install nodejs with nvm and run node with forever

Install nvm (node version manager)
curl https://raw.github.com/creationix/nvm/master/install.sh | sh nvm install 0.10.22 nvm list
使用 nvm vi .nvmrc
.nvmrc 0.10.22
進入 nodejs nvm use 0.10.22 #or num use 使用 npm sudo npm cache clean -f sudo npm install -g n npm install -g n sudo n stable sudo npm install -g coffee-script npm install -g forever 使用 package.json { "name": "counter", "version": "0.0.1", "private": true, "dependencies": { "forever": ">= 0.10.9", "mongodb": ">= 1.3.19", "router": ">= 0.6.2", "lodash": ">= 2.3.0", "async": ">= 0.2.9", "coffee-script": ">= 1.6.3" } } install nodejs package with package.json npm install 使用 forever # watch file change forever start -w server.js # list all process forever list # restart all forever restartall # check log forever logs server.js forever logs server.js -n 100




2013年7月2日

安裝 html2jade 筆記

原來竟然有 html2jade 這種東西,以前都是手動改寫,太花時間了

但是安裝過程意外的問題重重,以下筆記

首先nodejs 要更新,更新時才發現原來已經到 node v0.10.12了呀,我還在 v0.8.x 的時代

之後 npm 也要更新

python 要使用2.7以上,尤其要檢查/usr/local/bin/python 的版本對應,不然會出現 Import Error: 找不到 multiprocessor

然後python對應正確後要重新安裝node-gyp,非常重要的一點是,要先清除cache!


git clone http://github.com/isaacs/npm.git cd npm sudo make install

sudo npm rm -g node-gyp npm clean cache sudo npm install -g node-gyp sudo npm install -g html2jade





2011年6月6日

NodeJS

#install nodejs
wget http://nodejs.org/dist/node-v0.4.8.tar.gz
tar xf node-v0.4.8.tar.gz
cd node-v0.4.8
./configure
make
sudo make install
#and wait...
./node --version
sudo cp ./node /usr/local/bin
node --version
#install node package manager
curl http://npmjs.org/install.sh | sudo sh
#install required nodejs packages
npm install request http-agent htmlparser jsdom