您的位置:首页 > 运维架构 > Linux

Centos 7.2 Node.js 安装

2016-05-11 14:10 411 查看
Add some useful external repositories to install useful softwares.

[1]Install a plugin to add priorities to each installed repositories.
[root@dlp ~]# yum -y install yum-plugin-priorities

# set [priority=1] to official repository

[root@dlp ~]#sed -i -e "s/\]$/\]\npriority=1/g" /etc/yum.repos.d/CentOS-Base.repo

[2]Add EPEL Repository which is provided from Fedora project.
[root@dlp ~]# yum -y install epel-release

# set [priority=5]

[root@dlp ~]#sed -i -e "s/\]$/\]\npriority=5/g" /etc/yum.repos.d/epel.repo

# for another way, change to [enabled=0] and use it only when needed

[root@dlp ~]# sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/epel.repo

# if [enabled=0], input a command to use the repository

[root@dlp ~]#
yum --enablerepo=epel install [Package]

[3]Add RPMforge Repository which provides many useful packages.
[root@dlp ~]#
yum -y install http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm
# set [priority=10]

[root@dlp ~]# sed -i -e "s/\]$/\]\npriority=10/g" /etc/yum.repos.d/rpmforge.repo

# for another way, change to [enabled=0] and use it only when needed

[root@dlp ~]# sed -i -e "s/enabled = 1/enabled = 0/g" /etc/yum.repos.d/rpmforge.repo

# if [enabled=0], input a command to use the repository

[root@dlp ~]#
yum --enablerepo=rpmforge install [Package]

[root@www ~]#
yum --enablerepo=epel -y install nodejs npm

[2]Create a test tool. It's possible to do it as a common user.
[cent@www ~]$ vi helloworld.js

var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('listening on http://127.0.0.1:1337/');


# run server

[cent@www ~]$ node helloworld.js &

# verify (it's OK if following reply is back )

[cent@www ~]$ curl http://127.0.0.1:1337/
Hello World
[3]Install Socket.IO and create a test apprication with using WebSocket.
[cent@www ~]$ npm install socket.io express

[cent@www ~]$
vi chat.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});

http.listen(1337, function(){
console.log('listening on *:1337');
});


[cent@www ~]$
vi index.html

<!DOCTYPE html>
<html>
<head>
<title>WebSocket Chat</title>
</head>
<body>
<form action="">
<input id="sendmsg" autocomplete="off" /><button>Send</button>
</form>
<ul id="messages" style="list-style-type: decimal; font-size: 16px; font-family: Arial;"></ul>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#sendmsg').val());
$('#sendmsg').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li style="margin-bottom: 5px;">').text(msg));
});
</script>
</body>
</html>


[cent@www ~]$ node chat.js

listening on *:1337
Access to "http://(server's hostname or IP address):1337/" from a Client computer to make sure sample app works normally.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: