运维开发网
广告位招商联系QQ:123077622
 
广告位招商联系QQ:123077622

CentOS 7 配置JS语言开发环境(JavaScript)

运维开发网 https://www.qedev.com 2020-03-20 08:23 出处:网络 作者:运维开发网整理
CentOS 7 配置JS语言开发环境(JavaScript) 安装ServerSide JavaScript环境“Node.js” 安装Node.js和包管理工具npm

CentOS 7 配置JS语言开发环境(JavaScript)

安装ServerSide JavaScript环境“Node.js”

  • 安装Node.js和包管理工具npm
[root@Linuxprobe ~]# yum -y install epel-release
[root@Linuxprobe ~]# yum --enablerepo=epel -y install nodejs npm
  • 创建一个测试工具
[root@Linuxprobe ~]$ 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
[root@Linuxprobe ~]$ node helloworld.js &
# verify (it's OK if following reply is back )
[root@Linuxprobe ~]$ curl http://127.0.0.1:1337/
Hello World
  • 安装Socket.IO并使用WebSocket创建测试
[root@Linuxprobe ~]$ npm install socket.io express
[root@Linuxprobe ~]$ 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');
});

[root@Linuxprobe ~]$ 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>

[root@Linuxprobe ~]$ node chat.js
listening on *:1337
  • 客户端浏览器访问测试http://10.1.1.53:1337/

    CentOS 7 配置JS语言开发环境(JavaScript)

安装ServerSide JavaScript环境Node.js 4(LTS)

  • 可以从CentOS SCLo软件存贮库进行安装
# install from SCLo
[root@Linuxprobe ~]# yum --enablerepo=centos-sclo-rh -y install rh-nodejs4
  • 设置环境变量
# load environment variables
[root@Linuxprobe ~]# scl enable rh-nodejs4 bash
[root@Linuxprobe ~]# node -v
v4.4.2
[root@Linuxprobe ~]# which node
/opt/rh/rh-nodejs4/root/usr/bin/node
  • 设置登录时自动启用Node.js 4
[root@Linuxprobe ~]# vi /etc/profile.d/rh-nodejs4.sh
#!/bin/bash
source /opt/rh/rh-nodejs4/enable
export X_SCLS="`scl enable rh-nodejs4 'echo $X_SCLS'`"

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号