node.js 筆記

來源:互聯網
上載者:User

標籤: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 筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.