A simple tutorial on using node.js under Mac OS _node.js

Source: Internet
Author: User
Tags install homebrew md5 install node git clone couchdb

Here is a good node.js article great Nodejs intro, which will give you a very convenient introduction to Node.js and CouchDB, and give an example of the implementation of REST services to perform the CRUD operations of bookmarks, using CouchDB as a number According to the library.

This article describes installing under Mac OS X and starting to use Node.js, which takes about 30 minutes, where we will also install CouchDB and implement the CouchDB REST API.

This article assumes that you have Git installed on your machine, and if not, please refer to this article for installation.

Installing Node.js and NPM

The easiest way to do this is to use the Nodejs Download section page on the Node.js website and select the installer under Mac, which will install Node.js and NPM (Node Package Manager) on your machine.
After the installation is successful, you can use node and NPM commands.

Install CouchDB

Because this article needs to use CouchDB to store objects, you also need to install CouchDB.

Install CouchDB slightly troublesome, because we need to download the source code then compile I, before this need to install Homebrew, please execute the following command:

git clone https://github.com/mxcl/homebrew.git
CD homebrew/bin
brew install autoconf automake
Libtool Brew Install COUCHDB


Important tip: CouchDB prior to a problem may prevent you from installing, to fix this problem need to manually edit the ~/couch/homebrew/library/formula/couchdb.rb file, edited as follows:

Copy Code code as follows:
Require ' formula '

Class Couchdb < Formula
URL ' http://www.apache.org/dyn/closer.cgi?path=couchdb/source/1.1.1/apache-couchdb-1.1.1.tar.gz '
Homepage "http://couchdb.apache.org/"
MD5 ' CD126219B9CB69A4C521ABD6960807A6 '


Note that you need to delete the source in the URL, and the resulting modifications are as follows:

Copy Code code as follows:
Require ' formula '

Class Couchdb < Formula
URL ' http://www.apache.org/dyn/closer.cgi?path=couchdb/1.1.1/apache-couchdb-1.1.1.tar.gz '
Homepage "http://couchdb.apache.org/"
MD5 ' CD126219B9CB69A4C521ABD6960807A6 '

If the installation process is suspended, you will need to ctrl-c terminate and execute the following command to try again:

Copy Code code as follows:
./brew Install-v Couchdb

For more information on installing CouchDB on Mac OS X, read "Installing CouchDB on OS X.".

Once the CouchDB is compiled, we can execute it manually./couchdb to start it, you can open http://127.0.0.1:5984/_utils this address in the browser to verify the success of CouchDB installation.

Download Tutorials

Now that all the required software has been installed, we continue with the node.js example.

First we use Git to get the instance source

git clone https://github.com/indexzero/nodejs-intro.git
Creating a CouchDB Database
Before we start the tutorial we need to create a CouchDB database, make sure the CouchDB is started, and then create the database using the following command:

$ curl-x put HTTP://127.0.0.1:5984/PINPOINT-DEV10
{' OK ': true}

You can access the http://127.0.0.1:5984/_utils in the browser and you can see the newly created database.

There's also a great CouchDB guide.

Start Tutorial

The node JS instance is built in a modular way, and the Lib directory contains many modules, and server scripts are in the bin directory.

For example, to start the CouchDB tutorial, you can execute the following command in the bin directory:

./server-t 02couchdb-s

Where the-t parameter allows you to specify the module in the Lib directory to be executed, the-s parameter is used to set up the Pinpoint-dev database we just created.

Sys-util changes

Depending on the version of Node.js, you may see the following error or warning:

Copy Code code as follows:
$ node-v
V0.7.7-pre

$./server-t 02couchdb-s

node.js:247
Throw e; Process.nexttick error, or ' Error ' event on tick
^
Error:the "SYS" module is now called "Util".
At sys.js:1:69
At Nativemodule.compile (node.js:572:5)
At Function.require (node.js:540:18)
At Function._load (module.js:297:25)
At Module.require (module.js:357:17)
At require (MODULE.JS:373:17)
At Object. (/home/ubuntu/nodejs-intro/bin/server:3:11)
At Module._compile (module.js:444:26)
At Object.. JS (module.js:462:10)
At Module.load (module.js:351:32)

To avoid this problem, you need to replace all call ' require (' sys ') ' with ' Require ' ("util") '

Node v0.6.14 does not throw an error message, but it prompts a warning:

Copy Code code as follows:
$ node-v
v0.6.14

$./server-t 02couchdb-s
The "sys" module is now called "Util". It should have a similar interface.
Pinpoint demo server listening for 02couchdb on http://127.0.0.1:8000

Run the tutorial

When you run a tutorial, you will be prompted with some errors:


Copy Code code as follows:
$./server 02couchdb
The "sys" module is now called "Util". It should have a similar interface.

node.js:201
Throw e; Process.nexttick error, or ' Error ' event on tick
^
Error:cannot Find module ' optimist '
At Function._resolvefilename (module.js:332:11)
At Function._load (module.js:279:25)
At Module.require (module.js:354:17)
At require (MODULE.JS:370:17)
At Object. (/USERS/DDEWAELE/PROJECTS/NODE/NODEJS-INTRO/BIN/SERVER:5:12)
At Module._compile (module.js:441:26)
At Object.. JS (module.js:459:10)
At Module.load (module.js:348:31)
At Function._load (module.js:308:12)
At array.0 (module.js:479:10)

This tutorial contains a lot of dependencies and we need to use NPM to download these dependent packages.

Installing the node package

Node Packages (dependencies) can be installed through the NPM command, for example:

$ npm Install Optimist
npm http Get https://registry.npmjs.org/optimist
npm http https://registry.npmjs.org /optimist
npm http Get https://registry.npmjs.org/optimist/-/optimist-0.2.8.tgz
npm http https:// Registry.npmjs.org/optimist/-/optimist-0.2.8.tgz
npm http Get https://registry.npmjs.org/wordwrap
npm http Https://registry.npmjs.org/wordwrap
npm http GET https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.2.tgz
npm http https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.2.tgz
optimist@0.2.8. /node_modules/optimist
└──wordwrap@0.0.2


These packages will be installed in the Node_modules folder:

$ ls-l. /node_modules/Total
0
drwxr-xr-x ddewaele staff 1 18:54 Optimist


This article requires the following node packages to be installed:

NPM Install Winston
npm install cradle
npm Install journey
NPM Install Optimist

Run the tutorial

Go to the Bin directory and run the tutorial with the following command:

$./server-t 02couchdb-s the
"sys" module is now called "Util". It should have a similar interface.
Pinpoint demo server listening for 02couchdb on http://127.0.0.1:8000

Then open the browser to access Http://127.0.0.1:8000/bookmarks, you will see the following results:

Copy Code code as follows:
{"Bookmarks": []}

This means that the service has been started and running, and in order to add point test data to the CouchDB, we can use the Http-console console to access the REST service of CouchDB.

Install Http-console

There is a great tool to help you debug your service, the tool named Http-console, which you can use NPM to install:

sudo npm install-g http-console

You can then execute the tool on the command line, unfortunately when we execute the command The Times are wrong:

$ http-console
 
 
node.js:201
    throw e;//Process.nexttick error, or ' Error ' event on ' a ' tick
Uire.paths is removed. Use Node_modules folders, or the NODE_PATH environment variable instead.
  At Function. (module.js:378:11)
  At Object. (/usr/local/lib/node_modules/http-console/bin/http-console:6:8)
  At Module._compile (module.js:441:26) at
  Object. JS (module.js:459:10) at
  module.load (module.js:348:31) at
  function._load (module.js:308:12)
  at array.0 (module.js:479:10) at
  eventemitter._tickcallback (node.js:192:40)


Unfortunately, we also need to manually edit the/usr/local/lib/node_modules/http-console/bin/http-console file, and then delete the following line:

Copy Code code as follows:
Require.paths.unshift (Path.join (__dirname, ' ... ', ' lib '));

Now the http-console can be started without any parameters, it will be connected to http://localhost:8080, if you need to specify the server and port, pass it as the first parameter to Http-console.

Please note that we use the \json command here to set the correct content-type:

$ http-console http://127.0.0.1:8000 the
"sys" module is now called "Util". It should have a similar interface.
> Http-console 0.6.1
> Welcome, enter. Help if ' re lost.
> Connecting to 127.0.0.1 on port 8000.
 
http://127.0.0.1:8000/> \json
http://127.0.0.1:8000/>


Accessing the REST service

In Http-console, you only need to enter Get/bookmarks to perform a GET request:

http://127.0.0.1:8000/> get/bookmarks
http/1.1 OK
date:sun, April Apr 17:23:27 GMT
Server:journey /0.4.0
content-type:application/json;charset=utf-8
content-length:16
connection:keep-alive
 
{
  bookmarks: []
}


You can also use the JSON fragment to perform a POST request:

Http://127.0.0.1:8000/> Post/bookmarks ...
{"url": "Http://nodejs.org"}
http/1.1 OK
Date:thu, April Apr 11:45:55 GMT
server:journey/0.4.0
Content-type:application/json; Charset=utf-8
content-length:91
connection:keep-alive
 
{
  bookmark: {
    _id: ' Wd-g-1 ',
    resource: ' Bookmark ',
    URL: ' http://nodejs.org '
  }
}


Then execute the GET request again and you will see the newly inserted data:

http://127.0.0.1:8000/> get/bookmarks
http/1.1 OK
date:sun, April Apr 17:23:27 GMT
Server:journey /0.4.0
content-type:application/json;charset=utf-8
content-length:16
connection:keep-alive
 
{
  bookmarks: [
    {
      _rev: ' 1-cfced13a45a068e95daa04beff562360 ',
      _id: ' wd-g-1 ',
      resource: ' Bookmark ',
      URL: ' http://nodejs.org '
    }
  ]
}

Related Article

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.