Home > Web Front-end > JS Tutorial > A simple tutorial on using Node.js under Mac OS_node.js

A simple tutorial on using Node.js under Mac OS_node.js

WBOY
Release: 2016-05-16 15:53:14
Original
1888 people have browsed it

Here is a good introductory article on Node.js great nodejs intro , which will give you a very convenient introduction to Node.js and CouchDB, and give you an example to implement REST services for Perform CRUD operations on bookmarks, using CouchDB as the database.

This article will introduce how to install and start using Node.js under Mac OS X. This process will take about 30 minutes. We will also install CouchDB and implement the REST API based on CouchDB.

This article assumes that Git is already installed on your machine. If not, please refer to this article to install it.

Install node.js and npm

The easiest way is to go to the node.js official website through the nodejs download section page and select the installer under Mac, which will install Node.js and npm (node package manager).
After the installation is successful, you can use the node and npm commands.

Install CouchDB

Because this article requires CouchDB to store objects, CouchDB also needs to be installed.

Installing CouchDB is a little more troublesome, because we need to download the source code and then compile it. Before that, we 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
Copy after login


Important note: CouchDB previously reported a problem that may prevent you from installing it. To fix this problem, you need to manually edit the ~/couch/homebrew/Library/Formula/couchdb.rb file. The edit content is as follows:

Copy code The code is 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'


Please note that the source in the url needs to be deleted. The final modification result is as follows:

Copy code The code is 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 hangs, you need to CTRL-C to terminate and execute the following command to try again:

Copy code The code is as follows:
./brew install -v couchdb

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

Once CouchDB is compiled, we can manually execute ./couchdb to start it. You can open the address http://127.0.0.1:5984/_utils in your browser to verify whether the CouchDB installation is successful.

201562495503417.jpg (1009×575)

Download Tutorial

Now that the required software has been installed, let’s continue with the Node.js introduction example.

First we use Git to obtain the instance source code

git clone https://github.com/indexzero/nodejs-intro.git
Create CouchDB database
Before starting the tutorial, we need to create a CouchDB database. First ensure that CouchDB has been started, and then use the following command to create the database:

$ curl -X PUT http://127.0.0.1:5984/pinpoint-dev10
{"ok":true}

You can visit http://127.0.0.1:5984/_utils in your browser to see the newly created database.

There is also a great guide to CouchDB here.

Start tutorial

The node js instance is built in a modular way. The lib directory contains many modules, and the server script is in the bin directory.

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

./server -t 02couchdb -s

The -t parameter allows you to specify the module in the lib directory to be executed, and the -s parameter is used to set the pinpoint-dev database we just created.

sys - util changes

Depending on the version of Node.js, you may see the following errors or warnings:

Copy code The code is as follows:
$ node -v
v0.7.7-pre

$ ./server -t 02couchdb -s

node.js:247
           throw e; // process.nextTick error, or 'error' event on first 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 calls `require("sys")` with `require("util")`

Node v0.6.14 will not throw an error message, but will prompt a warning:

Copy code The code is 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, some errors will be prompted:


Copy code The code is 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 first 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)

该教程包含很多依赖,我们需要使用 npm 来下载这些依赖的包。

安装 node 包

Node packages (dependencies) 可通过 npm 命令来安装,例如:

$ npm install optimist
npm http GET https://registry.npmjs.org/optimist
npm http 200 https://registry.npmjs.org/optimist
npm http GET https://registry.npmjs.org/optimist/-/optimist-0.2.8.tgz
npm http 200 https://registry.npmjs.org/optimist/-/optimist-0.2.8.tgz
npm http GET https://registry.npmjs.org/wordwrap
npm http 200 https://registry.npmjs.org/wordwrap
npm http GET https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.2.tgz
npm http 200 https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.2.tgz
optimist@0.2.8 ../node_modules/optimist
└── wordwrap@0.0.2
Copy after login


这些包将被安装到 node_modules 文件夹中:

$ ls -l ../node_modules/
total 0
drwxr-xr-x 10 ddewaele staff 340 Apr 1 18:54 optimist
Copy after login


本文需要安装如下的 node 包:

npm install winston
npm install cradle
npm install journey
npm install optimist
Copy after login

运行教程

进入 bin 目录,通过下面命令来运行教程:

$ ./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
Copy after login

然后打开浏览器访问 http://127.0.0.1:8000/bookmarks ,将会看到如下的结果:

复制代码 代码如下:
{"bookmarks":[]}

这表示服务已经启动并运行,为了在 CouchDB 中添加点测试数据,我们可以使用 http-console 控制台来访问 CouchDB 的 REST 服务。

安装 http-console

有一个非常棒的工具可以帮助你调试服务,该工具名为 http-console ,你可使用 npm 来安装:

sudo npm install -g http-console
Copy after login

然后就可以在命令行中执行该工具,不幸的是当我们执行该命令时报错了:

$ http-console
 
 
node.js:201
    throw e; // process.nextTick error, or 'error' event on first tick
       ^
Error: require.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)
Copy after login


很麻烦,我们还需要手工编辑 /usr/local/lib/node_modules/http-console/bin/http-console 文件,然后删除下面这一行:

复制代码 代码如下:
require.paths.unshift(path.join(__dirname, '..', 'lib'));

现在 http-console 就可以启动了,无需任何参数,它将连接到 http://localhost:8080 ,如果你需要指定服务器和端口,把它作为第一个参数传递给 http-console 即可。

请注意我们这里使用了 \json 命令用来设置正确的 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 you're lost.
> Connecting to 127.0.0.1 on port 8000.
 
http://127.0.0.1:8000/> \json
http://127.0.0.1:8000/>

Copy after login

访问 REST 服务

在 http-console 中,要执行 GET 请求只需要输入 GET /bookmarks 即可:

http://127.0.0.1:8000/> GET /bookmarks
HTTP/1.1 200 OK
Date: Sun, 01 Apr 2012 17:23:27 GMT
Server: journey/0.4.0
Content-Type: application/json;charset=utf-8
Content-Length: 16
Connection: keep-alive
 
{
  bookmarks: []
}
Copy after login


你也可以使用 JSON 的片段来执行 POST 请求:

http://127.0.0.1:8000/> POST /bookmarks
... { "url": "http://nodejs.org" }
HTTP/1.1 200 OK
Date: Thu, 05 Apr 2012 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'
  }
}
Copy after login


然后再次执行 GET 请求,你就可以看到新插入的数据了:

http://127.0.0.1:8000/> GET /bookmarks
HTTP/1.1 200 OK
Date: Sun, 01 Apr 2012 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'
    }
  ]
}
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template