1.建立新專案︰
$ express --view=pug nodeservice create : nodeservice create : nodeservice/package.json create : nodeservice/app.js create : nodeservice/public create : nodeservice/public/stylesheets create : nodeservice/public/stylesheets/style.css create : nodeservice/public/images create : nodeservice/routes create : nodeservice/routes/index.js create : nodeservice/routes/users.js create : nodeservice/public/javascripts create : nodeservice/views create : nodeservice/views/index.pug create : nodeservice/views/layout.pug create : nodeservice/views/error.pug create : nodeservice/bin create : nodeservice/bin/www install dependencies: $ cd nodeservice && npm install run the app: $ DEBUG=nodeservice:* npm start
2.安裝相依項目(install dependencies 同上面所寫)
$ cd nodeservice && npm install
以下是目前會用的到,也一起安裝
npm install date-utils --save npm install fs --save npm install quick-local-ip --save npm install mongodb --save
3.在console.log上加上時間
在app.js上增加
require('date-utils'); // log override console.log=(function() { var orig = console.log; return function() { try { var tmp=process.stdout; process.stdout=process.stderr; // get now var now = new Date(); // format time var now_formated = now.toFormat("YYYY-MM-DD HH24:MI:SS"); var strDate = '[' + now_formated + '] '; if (Buffer.isBuffer(arguments[0])) { arguments[0] = strDate + arguments[0]; } else if (typeof arguments[0] !== 'object') { arguments[0] = strDate + arguments[0]; } else if (typeof arguments[0] === 'object') { arguments[0] = strDate + JSON.stringify(arguments[0]); } orig.apply(console, arguments); } finally { process.stdout=tmp; } }; })();
4.啟動Server時在Log上印出server的url︰
在bin/www增加
var myip = require('quick-local-ip'); function onListening() { var addr = server.address(); var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; debug('Local IP: ' + myip.getLocalIP4() + ' Listening on ' + bind); debug('http://' + myip.getLocalIP4() + ':' + addr.port); }
5.啟動Server
$ DEBUG=nodeservice:* npm start > nodeservice@0.0.0 start /Users/kirk/Documents/NodeProjects/nodeservice > node ./bin/www nodeservice:server Local IP: 192.168.3.35 Listening on port 3000 +0ms nodeservice:server http://192.168.3.35:3000 +2ms
6.取資料
//HttpHeader: var udid = req.headers['udid']; //Post Attribute: var product = req.body.product; //Get QueryString: router.get('/WTXO', function(req, res, next) var m = req.query.m; //Get from RESTFUL Url router.get('/WTXO/:t', function(req, res, next) var month = req.params.t;
以上。