欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在Linux系統(tǒng)中搭建Node.js開發(fā)環(huán)境的簡單步驟講解

 更新時間:2016年01月26日 16:13:42   作者:William_Sang  
這篇文章主要介紹了在Linux系統(tǒng)中搭建Node.js開發(fā)環(huán)境的步驟,Node使得JavaScript程序可以在本地操作系統(tǒng)環(huán)境中解釋運行,需要的朋友可以參考下

1. Linux安裝node.js

ubuntu:

sudo apt-get install nodejs npm

centos:

yum install nodejs npm

更詳細的安裝參見:https://github.com/joyent/node/wiki/Installation
npm為類似PHP中Pear的包管理器

2. 開始使用node.js

用文本編輯器新建hello.js寫入以下內(nèi)容

console.log('hello world');

打開命令行輸入

$ node hello.js

你會看到輸出  

 $ hello world

    console.log是最常用的輸出指令

3. 建立HTTP服務(wù)器

理解node.js架構(gòu)
像PHP的架構(gòu)模型為:
    瀏覽器--》HTTP服務(wù)器(apache、nginx)--》PHP解釋器

而在node.js應(yīng)用中,node.js采用:
    瀏覽器--》node.js這種架構(gòu)

創(chuàng)建HTTP服務(wù)器:新建一個app.js文件,內(nèi)容如下:

var http = require('http');
http.createServer(function(req, res){
  res.writeHead(200,{'Content-Type': 'text/html'});
  res.write('</pre>
<h1>node.js</h1>
<pre>');
  res.end('
hello world
 
');
}).listen(3000);
console.log("http server is listening at port 3000.");

運行

$ node app.js

打開瀏覽器打開http://127.0.0.1:3000查看結(jié)果

該程序調(diào)用了node.js提供的http模塊,對所有的Http請求答復同樣的內(nèi)容并監(jiān)聽3000端口。運行這個腳本后不會立刻退出,必須按下ctro+c才會停止,這是因為listen函數(shù)創(chuàng)建了事件監(jiān)聽器。

4. 調(diào)試腳本

node.js腳本修改后,必須停止原程序,重新運行,才能看到變化。
用包管理器安裝supervisor工具。

$ npm install -g supervisor

以后通過

$ supervisor app.js

來運行node.js程序,它會檢測程序代碼變化,自動重啟程序。
注意:安裝時需要獲得root權(quán)限。

相關(guān)文章

最新評論