2014年7月14日 星期一

Centos6.5中安裝node.js並使用Express執行node App

晚上9:25:00 Posted by Amos , No comments

簡介

本文概述了簡單的步驟,教導如何安裝node.js並於該環境中執行一個 Hello World Applicaiton,租用Centos6.4 64位元版本的雲端伺服器進行測試及安裝,Node.js主要是採用Chrome JavaScript之運行引擎,是一個快速,事件驅動的平台和運行於服務器端之Javascript 語言,用於構建Web應用程式。

環境介紹及前置作業

作業系統版本 : Centos 6.5 (64位元)

安裝編輯及wget套件

若於Centos中已有安裝vim (編輯工具)和wget(抓檔案工具)時,可以忽略該步驟
yum -y install wget vim

關閉SELinux

編輯 /etc/sysconfig/selinux檔案,將SELINUX=enforcing 改成 SELINUX=disabled,重新開機。
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing 改成 SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

開啟防火牆

編輯 /etc/sysconfig/iptables檔案,開啟8080防火牆(用於node.js的服務),設定完成後請重新啟動防火牆
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
/etc/init.d/iptables restart

設定主機執行服務之open file的個數

編輯/etc/security/limits.conf檔案,輸入以下指令並存檔
* hard nofile 65535
* soft nofile 65535

更改主機(hostname)名稱

編輯 /etc/sysconfig/network檔案,輸入hostname的名稱(如: localhost.localdomain )
localhost.localdomain
編輯 /etc/hosts檔案,修改如下
127.0.0.1       localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6

安裝 Node.js

下載EPEL和REMI擴充套件,並使用rpm安裝套件
wget https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -ivh epel-release-6-8.noarch.rpm
rpm -ivh remi-release-6.rpm

使用 yum 指令安裝 node.js 和 NPM套件管理工具,npm主要提供node.js許多好用的第三方lib套件
yum install nodejs
yum install npm

安裝 Express.js

現在,我們已經將Node.js的安裝完成,可以開始開發及部署已完成的應用程式。首先,我們將使用NPM,安裝express套件。

 npm -g install express express-generator supervisor
安裝完成後將會顯示安裝之目錄結構與檔案,如下
supervisor@0.6.0 /usr/lib/node_modules/supervisor 

express-generator@4.2.0 /usr/lib/node_modules/express-generator
├── mkdirp@0.3.5
└── commander@1.3.2 (keypress@0.1.0)

express@4.6.1 /usr/lib/node_modules/express
├── merge-descriptors@0.0.2
├── utils-merge@1.0.0
├── parseurl@1.1.3
├── cookie@0.1.2
├── escape-html@1.0.1
├── finalhandler@0.0.3
├── cookie-signature@1.0.4
├── range-parser@1.0.0
├── fresh@0.2.2
├── vary@0.1.0
├── qs@0.6.6
├── media-typer@0.2.0
├── methods@1.1.0
├── serve-static@1.3.2
├── buffer-crc32@0.2.3
├── depd@0.3.0
├── path-to-regexp@0.1.3
├── type-is@1.3.2 (mime-types@1.0.1)
├── accepts@1.0.7 (negotiator@0.4.7, mime-types@1.0.1)
├── proxy-addr@1.0.1 (ipaddr.js@0.1.2)
├── debug@1.0.3 (ms@0.6.2)
└── send@0.6.0 (ms@0.6.2, mime@1.2.11, finished@1.2.2)

於這個指令中的 -g 參數的意思是指安裝至global位置(本安裝位置位於:/usr/lib/node_modules)。

建立一個Express架構的 Web Application

Express是一個Web App Framework,提供許多功能(如:restful等),首先使用以下指令建立express framework的專案名稱為: hello
express express-helloworld 

create : express-helloworld
create : express-helloworld/package.json
create : express-helloworld/app.js
create : express-helloworld/public
create : express-helloworld/public/javascripts
create : express-helloworld/public/images
create : express-helloworld/public/stylesheets
create : express-helloworld/public/stylesheets/style.css
create : express-helloworld/routes
create : express-helloworld/routes/index.js
create : express-helloworld/routes/users.js
create : express-helloworld/views
create : express-helloworld/views/index.jade
create : express-helloworld/views/layout.jade
create : express-helloworld/views/error.jade
create : express-helloworld/bin
create : express-helloworld/bin/www
install dependencies:
$ cd express-helloworld && npm install

run the app:
$ DEBUG=express-helloworld ./bin/www

建立完成後將會產生如下的檔案結構,檔案結構說明如下:
app.js : 主程式
bin : 內有www的檔案,負責啟動node.js server的檔案,該檔案會呼叫app.js
package.json : 專案設定檔案,紀錄專案需要使用到的套件,透過輸入 npm install 指令可自動下載相關套件
public : 存放靜態網頁 (CSS,Javascript,圖檔等)
routes : 設定restful網頁路徑js檔案,屬於MVC中的Control部分
views : 存放顯示UI的網頁,預設使用Jude Template設計網頁UI,屬於MVC中的View部分

切換目錄至 express-helloworld 資料夾,並執行以下指令安裝相關Framework套件
 
cd express-helloworld
npm install

切換目錄至 express-helloworld/bin 資料夾,編輯www檔案,將3000 Port 改成 8080 Port
app.set('port', process.env.PORT || 3000); 修改成 
app.set('port', process.env.PORT || 8080);

最後執行以下指令啟動node.js應用程式
 node ./bin/www 
現在我們就可以開啟瀏覽器執行 http://雲端伺服器的IP:8080/

0 意見:

張貼留言