Installing Nodejs
nodejs.org
node. js for MAC Installation
Installing NPM
curl http://npmjs.org/install.sh | sh
Update NPM
sudo npm update npm -g
Installing PM2
npm install pm2 -g
Update PM2
pm2 update
Install Express
$ npm install -g express-generator #需先安装express-generator $ npm install -g express $ express -V #验证是否安装成功
Installing Restify
Install restify to the directory Restify-dmeo
$ mkdir restify-demo$ cd restify-demo$ npm install restify
Error during installation no such file or directory, open '/users/xxx/package.json '
This hint does not affect the test at this time.
Write Test 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
Installing MongoDB
brew install mongodb
npm link mongodb
npm install node-gyp -g && npm cache clean && rm -rf node_modules && npm install
Start MongoDB
mongod —config /usr/local/etc/mongod.conf
NodeJS using UUID
UUID, you must first install the module node-uuid for NodeJS:
npm install uuid
Then generate the UUID and use it when you insert:
var uuid = require(‘node-uuid‘);
Connect to 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 ', & nbsp email: ' [email protected] ', tel: [ & nbsp ' 0926xxx5xx ', ' 0912xx11xx ' ] }, function (err, data) { if (data) { & nbsp console.log (' Successfully insert '); }&NB sp;else { console.log (' Failed to insert '); } }); /* QUERYING */&NB Sp Collection.find ({ name: ' Fred chien ' }, function (err, data) { /* found this people */ if (data) { console.log (' name: ' & nbsp;+ data.name + ', email: ' + data.email); } else { &NBSp console.log (' Cannot found '); } &NBS P ); });
Mongous-A lightweight Nodejs MongoDB driver
https://github.com/amark/mongous
Sublime Text Writing JS Tool
node. JS Notes