最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在Mac OS下使用Node.js的簡單教程

 更新時(shí)間:2015年06月24日 10:09:36   投稿:goldensun  
這篇文章主要介紹了在Mac OS下使用Node.js的簡單教程,Node.js是讓JavaScript應(yīng)用運(yùn)行于服務(wù)器端的框架,需要的朋友可以參考下

這里有一篇很好的 Node.js 介紹文章 great nodejs intro ,它將給你一個(gè)非常方便的介紹 Node.js 和 CouchDB,并給出一個(gè)實(shí)例實(shí)現(xiàn) REST 的服務(wù)用于執(zhí)行書簽的 CRUD 操作,使用 CouchDB 作為數(shù)據(jù)庫。

本文將介紹在 Mac OS X 下安裝并開始使用 Node.js ,這個(gè)過程大概需要 30 分鐘左右的時(shí)間,其中我們還將安裝 CouchDB,并實(shí)現(xiàn)基于 CouchDB 的 REST API。

本文假設(shè)你機(jī)器上已經(jīng)裝有Git,如果還沒有,請(qǐng)參考此文進(jìn)行安裝。

安裝 node.js 和 npm

最簡單的方法是在 node.js 的官網(wǎng)上通過 the nodejs download section 頁面并選擇 Mac 下的安裝程序,它將在你的機(jī)器上安裝 Node.js 和 npm (node package manager). 
 安裝成功后你就可以使用 node 和 npm 命令了。

安裝 CouchDB

因?yàn)楸疚男枰褂?CouchDB 來存儲(chǔ)對(duì)象,因此還需要安裝 CouchDB.

安裝 CouchDB 稍微麻煩一些,因?yàn)槲覀冃枰螺d源碼然后編譯I,在此之前需要先安裝 Homebrew ,請(qǐng)執(zhí)行以下命令:
 

git clone https://github.com/mxcl/homebrew.git
cd homebrew/bin
brew install autoconf automake libtool
brew install couchdb


重要的提示:CouchDB 之前報(bào)出一個(gè)問題可能會(huì)阻止你安裝,要修復(fù)這個(gè)問題需要手工編輯 ~/couch/homebrew/Library/Formula/couchdb.rb 文件,編輯內(nèi)容如下:
 

復(fù)制代碼 代碼如下:
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'


請(qǐng)注意需要將 url 中的 source 刪除,最終修改結(jié)果如下:
 

復(fù)制代碼 代碼如下:
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 終止并執(zhí)行下面命令重試:
 

復(fù)制代碼 代碼如下:
./brew install -v couchdb

更多關(guān)于 Mac OS X 上安裝 CouchDB 的信息請(qǐng)閱讀 "Installing CouchDB on OSX".

一旦 CouchDB 編譯完成,我們可以手工執(zhí)行 ./couchdb 來啟動(dòng)它,你可以在瀏覽器中打開 http://127.0.0.1:5984/_utils 這個(gè)地址以驗(yàn)證 CouchDB 安裝是否成功。

201562495503417.jpg (1009×575)

 下載教程

現(xiàn)在所需的軟件都已經(jīng)安裝完成,我們接下來繼續(xù) Node.js 的介紹實(shí)例。

首先我們使用 Git 來獲取實(shí)例源碼
 
git clone https://github.com/indexzero/nodejs-intro.git
創(chuàng)建 CouchDB 數(shù)據(jù)庫
在開始教程之前我們需要?jiǎng)?chuàng)建一個(gè) CouchDB 數(shù)據(jù)庫,先確保 CouchDB 已經(jīng)啟動(dòng),然后使用如下命令創(chuàng)建數(shù)據(jù)庫:
 
$ curl -X PUT http://127.0.0.1:5984/pinpoint-dev10
{"ok":true}

你可以在瀏覽器中訪問 http://127.0.0.1:5984/_utils 就可以看到新創(chuàng)建的數(shù)據(jù)庫。

這里還有一個(gè)非常棒的 CouchDB 的指南。

開始教程

node js 實(shí)例使用模塊化的方式構(gòu)建,lib 目錄包含很多模塊,而服務(wù)器腳本在 bin 目錄下。

例如,我們要啟動(dòng) CouchDB 教程,可以在 bin 目錄下執(zhí)行下面命令:
 
./server -t 02couchdb -s

其中 -t 參數(shù)允許你指定要執(zhí)行的 lib 目錄下的模塊,-s 參數(shù)用以設(shè)置我們剛建立的 pinpoint-dev 數(shù)據(jù)庫。

sys - util 變化

根據(jù) Node.js 的版本不同,你可能會(huì)看到如下的錯(cuò)誤或者是警告:
 

復(fù)制代碼 代碼如下:
$ 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)

為了避免這個(gè)問題,你需要將所有調(diào)用 `require("sys")` 替換成 `require("util")`

Node v0.6.14 不會(huì)拋出錯(cuò)誤信息,但會(huì)提示警告:
 

復(fù)制代碼 代碼如下:
$ 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

運(yùn)行教程

當(dāng)你運(yùn)行某個(gè)教程時(shí),會(huì)提示一些錯(cuò)誤:

 

復(fù)制代碼 代碼如下:
$ ./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

運(yùn)行教程

進(jìn)入 bin 目錄,通過下面命令來運(yùn)行教程:
 

$ ./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 ,將會(huì)看到如下的結(jié)果:
 

復(fù)制代碼 代碼如下:
{"bookmarks":[]}

這表示服務(wù)已經(jīng)啟動(dòng)并運(yùn)行,為了在 CouchDB 中添加點(diǎn)測(cè)試數(shù)據(jù),我們可以使用 http-console 控制臺(tái)來訪問 CouchDB 的 REST 服務(wù)。

安裝 http-console

有一個(gè)非常棒的工具可以幫助你調(diào)試服務(wù),該工具名為 http-console ,你可使用 npm 來安裝:
 

sudo npm install -g http-console

然后就可以在命令行中執(zhí)行該工具,不幸的是當(dāng)我們執(zhí)行該命令時(shí)報(bào)錯(cuò)了:
 

$ 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 文件,然后刪除下面這一行:
 

復(fù)制代碼 代碼如下:
require.paths.unshift(path.join(__dirname, '..', 'lib'));

現(xiàn)在 http-console 就可以啟動(dòng)了,無需任何參數(shù),它將連接到 http://localhost:8080 ,如果你需要指定服務(wù)器和端口,把它作為第一個(gè)參數(shù)傳遞給 http-console 即可。

請(qǐng)注意我們這里使用了 \json 命令用來設(shè)置正確的 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 服務(wù)

在 http-console 中,要執(zhí)行 GET 請(qǐng)求只需要輸入 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 的片段來執(zhí)行 POST 請(qǐng)求:
 

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'
  }
}


然后再次執(zhí)行 GET 請(qǐng)求,你就可以看到新插入的數(shù)據(jù)了:
 

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'
    }
  ]
}

相關(guān)文章

  • node中的Express框架詳解

    node中的Express框架詳解

    這篇文章主要介紹了node中的Express框架,框架是為了規(guī)范開發(fā)流程,降低開發(fā)難度,提高開發(fā)效率而制定的一套共人們使用的功能模塊或者是編程的約定,需要的朋友可以參考下
    2023-04-04
  • nodejs如何讀取文件二進(jìn)制 前端響應(yīng)blob或base64顯示圖片

    nodejs如何讀取文件二進(jìn)制 前端響應(yīng)blob或base64顯示圖片

    這篇文章主要介紹了nodejs如何讀取文件二進(jìn)制 前端響應(yīng)blob或base64顯示圖片方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 阿里云ecs服務(wù)器中安裝部署node.js的步驟

    阿里云ecs服務(wù)器中安裝部署node.js的步驟

    這篇文章給大家介紹了在阿里云ecs服務(wù)器中安裝部署node.js的詳細(xì)步驟,對(duì)大家安裝node.js具有一定的參考借鑒價(jià)值,有需要的朋友們下面來一起看看吧。
    2016-10-10
  • nvm管理node無法正常切換node版本問題的解決方法

    nvm管理node無法正常切換node版本問題的解決方法

    相信一定會(huì)有存在一些小伙伴 明明都已經(jīng)按著操作卸載node 和安裝nvm 了但是 依舊無法正常通過nvm管理node,本文將給大家介紹nvm管理node無法正常切換node版本問題的解決方法,需要的朋友可以參考下
    2024-01-01
  • 使用Make構(gòu)建Node.js網(wǎng)站項(xiàng)目

    使用Make構(gòu)建Node.js網(wǎng)站項(xiàng)目

    這篇文章介紹了使用Make構(gòu)建Node.js網(wǎng)站項(xiàng)目的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01
  • 詳解使用Typescript開發(fā)node.js項(xiàng)目(簡單的環(huán)境配置)

    詳解使用Typescript開發(fā)node.js項(xiàng)目(簡單的環(huán)境配置)

    本篇文章主要介紹了詳解使用Typescript開發(fā)node.js項(xiàng)目(簡單的環(huán)境配置),非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-10-10
  • Node.js進(jìn)程管理之進(jìn)程集群詳解

    Node.js進(jìn)程管理之進(jìn)程集群詳解

    這篇文章介紹了Node.js進(jìn)程管理之進(jìn)程集群,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • 一文帶你了解Node.js進(jìn)程管理工具PM2

    一文帶你了解Node.js進(jìn)程管理工具PM2

    Node.js進(jìn)程管理工具PM2是一個(gè)開源的工具,用于管理和監(jiān)控Node.js應(yīng)用程序的運(yùn)行,它可以幫助您方便地啟動(dòng)、停止、重啟和監(jiān)視多個(gè)Node.js進(jìn)程,并提供了許多有用的功能,所以本文就和大家一起了解一下PM2,需要的朋友可以參考下
    2023-07-07
  • node.js開機(jī)自啟動(dòng)腳本文件

    node.js開機(jī)自啟動(dòng)腳本文件

    這篇文章主要介紹了node.js開機(jī)自啟動(dòng)腳本文件的方法和代碼,這里分享給大家,有需要的小伙伴參考下吧
    2014-12-12
  • node.js中fs文件系統(tǒng)模塊的使用方法實(shí)例詳解

    node.js中fs文件系統(tǒng)模塊的使用方法實(shí)例詳解

    這篇文章主要介紹了node.js中fs文件系統(tǒng)模塊的使用方法,結(jié)合實(shí)例形式詳細(xì)分析了node.js fs文件系統(tǒng)模塊各種常見方法的基本使用技巧與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02

最新評(píng)論

峡江县| 汉沽区| 交城县| 信宜市| 堆龙德庆县| 富川| 济源市| 延长县| 时尚| 凌海市| 鄂伦春自治旗| 南充市| 四川省| 甘洛县| 皮山县| 馆陶县| 兴和县| 顺义区| 临漳县| 比如县| 依安县| 伊金霍洛旗| 滦南县| 留坝县| 康保县| 永昌县| 资阳市| 衡南县| 文登市| 和顺县| 甘孜县| 林口县| 英吉沙县| 安龙县| 潜江市| 阿城市| 吐鲁番市| 丘北县| 惠安县| 手机| 章丘市|