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

$ 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 :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 :8000

然后打开浏览器访问 :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 就可以启动了,无需任何参数,它将连接到 :8080 ,如果你需要指定服务器和端口,把它作为第一个参数传递给 http-console 即可。

请注意我们这里使用了 \json 命令用来设置正确的 content-type:
 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgdjsf.html