標籤:date pac ann hello 更新 use sub etc can
安裝nodejs
nodejs.org
Node.js for Mac 安裝
安裝npm
curl http://npmjs.org/install.sh | sh
更新npm
sudo npm update npm -g
安裝 PM2
npm install pm2 -g
更新PM2
pm2 update
安裝 express
$ npm install -g express-generator #需先安裝express-generator $ npm install -g express $ express -V #驗證是否安裝成功
安裝restify
將restify安裝到目錄restify-dmeo
$ mkdir restify-demo$ cd restify-demo$ npm install restify
安裝時出錯 no such file or directory, open ‘/Users/XXX/package.json‘
此提示在此時測試沒有影響.
編寫測試js
cd Desktop/vi test.js
var restify = require(‘restify‘); //引入包function respond(req, res, next) { res.send(‘hello ‘ + req.params.name); next();}var server = restify.createServer();server.get(‘/hello/:name‘, respond); //訪問的路徑server.head(‘/hello/:name‘, respond);server.listen(8080, function() { //8080 連接埠 console.log(‘%s listening at %s‘, server.name, server.url);});
pm2 start test.js -i 1 --name=test -f
pm2 stop test.js
安裝MongoDb
brew install mongodb
npm link mongodb
npm install node-gyp -g && npm cache clean && rm -rf node_modules && npm install
啟動MongoDb
mongod —config /usr/local/etc/mongod.conf
NodeJS 使用UUID
UUID,必需先為 NodeJS 安裝模組 node-uuid:
npm install uuid
然後產生 UUID 並在 insert 時使用:
var uuid = require(‘node-uuid‘);
串連MongoDB
var mongodb = require(‘mongodb‘);var mongodbServer = new mongodb.Server(‘localhost‘, 27017, { auto_reconnect: true, poolSize: 10 });var db = new mongodb.Db(‘mydb‘, mongodbServer);/* open db */db.open(function() { /* Select ‘contact‘ collection */ db.collection(‘contact‘, function(err, collection) { /* Insert a data */ collection.insert({ name: ‘Fred Chien‘, email: ‘[email protected]‘, tel: [ ‘0926xxx5xx‘, ‘0912xx11xx‘ ] }, function(err, data) { if (data) { console.log(‘Successfully Insert‘); } else { console.log(‘Failed to Insert‘); } }); /* Querying */ collection.find({ name: ‘Fred Chien‘ }, function(err, data) { /* Found this People */ if (data) { console.log(‘Name: ‘ + data.name + ‘, email: ‘ + data.email); } else { console.log(‘Cannot found‘); } }); });});
Mongous - 一個輕量級的nodejs mongodb驅動
https://github.com/amark/mongous
sublime Text 編寫js 工具
node.js 筆記