A5下载站:努力做内容最丰富最安全的下载站! 网站地图最新更新下载排行专题软件发布

热门软件

地铁跑酷

冒险迷岛

全民迷宫

连连消大作战

小河狸创客

阿里健康医鹿

支付宝app

番薯小说

MOMO陌陌

虾米音乐app

位置导航:A5下载 > 源码技巧 > 父类数据

在Mac OS下使用Node.js的简单教程

时间:2015-06-24 11:49来源:a5源码作者:zhao浏览:24
这篇文章主要介绍了在Mac OS下使用Node.js的简单教程,Node.js是让JavaScript应用运行于服务器端的框架,需要的朋友可以参考下……

这里有一篇很好的 Node.js 介绍文章 great nodejs intro ,它将给你一个非常方便的介绍 Node.js 和 CouchDB,并给出一个实例实现 REST 的服务用于执行书签的 CRUD 操作,使用 CouchDB 作为数据库。

本文将介绍在 Mac OS X 下安装并开始使用 Node.js ,这个过程大概需要 30 分钟左右的时间,其中我们还将安装 CouchDB,并实现基于 CouchDB 的 REST API。

本文假设你机器上已经装有Git,如果还没有,请参考此文进行安装。

安装 node.js 和 npm

最简单的方法是在 node.js 的官网上通过 the nodejs download section 页面并选择 Mac 下的安装程序,它将在你的机器上安装 Node.js 和 npm (node package manager).

安装成功后你就可以使用 node 和 npm 命令了。

安装 CouchDB

因为本文需要使用 CouchDB 来存储对象,因此还需要安装 CouchDB.

安装 CouchDB 稍微麻烦一些,因为我们需要下载源码然后编译I,在此之前需要先安装 Homebrew ,请执行以下命令:

git clone https://github.com/mxcl/homebrew.git

cd homebrew/bin

brew install autoconf automake libtool

brew install couchdb

重要的提示:CouchDB 之前报出一个问题可能会阻止你安装,要修复这个问题需要手工编辑 ~/couch/homebrew/Library/Formula/couchdb.rb 文件,编辑内容如下:

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'

请注意需要将 url 中的 source 删除,最终修改结果如下:

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'

如果安装过程被挂起了,你需要 CTRL-C 终止并执行下面命令重试:

./brew install -v couchdb

更多关于 Mac OS X 上安装 CouchDB 的信息请阅读 "Installing CouchDB on OSX".

一旦 CouchDB 编译完成,我们可以手工执行 ./couchdb 来启动它,你可以在浏览器中打开 http://127.0.0.1:5984/_utils 这个地址以验证 CouchDB 安装是否成功。

在Mac OS下使用Node.js的简单教程

下载教程

现在所需的软件都已经安装完成,我们接下来继续 Node.js 的介绍实例。

首先我们使用 Git 来获取实例源码

git clone https://github.com/indexzero/nodejs-intro.git

创建 CouchDB 数据库

在开始教程之前我们需要创建一个 CouchDB 数据库,先确保 CouchDB 已经启动,然后使用如下命令创建数据库:

$ curl -X PUT http://127.0.0.1:5984/pinpoint-dev10

{"ok":true}

你可以在浏览器中访问 http://127.0.0.1:5984/_utils 就可以看到新创建的数据库。

这里还有一个非常棒的 CouchDB 的指南。

开始教程

node js 实例使用模块化的方式构建,lib 目录包含很多模块,而服务器脚本在 bin 目录下。

例如,我们要启动 CouchDB 教程,可以在 bin 目录下执行下面命令:

./server -t 02couchdb -s

其中 -t 参数允许你指定要执行的 lib 目录下的模块,-s 参数用以设置我们刚建立的 pinpoint-dev 数据库。

sys - util 变化

根据 Node.js 的版本不同,你可能会看到如下的错误或者是警告:

$ 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)

为了避免这个问题,你需要将所有调用 `require("sys")` 替换成 `require("util")`

Node v0.6.14 不会抛出错误信息,但会提示警告:

$ 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

运行教程

当你运行某个教程时,会提示一些错误:

$ ./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

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

$ ls -l ../node_modules/

total 0

drwxr-xr-x 10 ddewaele staff 340 Apr 1 18:54 optimist

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

npm install winston

npm install cradle

npm install journey

npm install optimist

运行教程

进入 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

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

{"bookmarks":[]}

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

安装 http-console

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

sudo npm install -g http-console

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

$ 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)

很麻烦,我们还需要手工编辑 /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/>

访问 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: []

}

你也可以使用 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'

}

}


然后再次执行 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'

}

]

}