1. 程式人生 > >CentOS 環境搭建並測試Node.js伺服器開發環境

CentOS 環境搭建並測試Node.js伺服器開發環境

安裝

使用root使用者:

[xiaoqw@VM_42_160_centos ~]$ yum install nodejs
[xiaoqw@VM_42_160_centos ~]$ yum install npm

CentOS 6.5 騰訊雲主機安裝後不用配置環境變數,直接可用。

[xiaoqw@VM_42_160_centos ~]$ node --version
v0.10.48

基礎測試

[xiaoqw@VM_42_160_centos nodejs]$ pwd
/home/xiaoqw/nodejs
[xiaoqw@VM_42_160_centos nodejs]$ cat node.js
console.log("Hello World"
); # VIM開啟node.js檔案,編寫本行內容 [xiaoqw@VM_42_160_centos nodejs]$ node node.js Hello World #成功輸出,說明最基本的開發環境是可以了。

建立一個Server應用

var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain});
    response.end('Hello World\n');
}).listen(8080);

console.log('
Server running at http://yixzm.cn:8080/');

執行結果:

[xiaoqw@VM_42_160_centos nodejs]$ node server.js
Server running at http://yixzm.cn:8080/