node. JS Notes

Source: Internet
Author: User
Tags brew install mongodb install mongodb mongodb driver sublime text

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.