登录  /  注册
首页 > web前端 > js教程 > 正文

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

php中文网
发布: 2016-05-16 15:53:14
原创
1635人浏览过

这里有一篇很好的 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 安装是否成功。

201562495503417.jpg (1009×575)

下载教程

现在所需的软件都已经安装完成,我们接下来继续 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'
    }
  ]
}
登录后复制

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号