前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CentOS 7 配置JS语言开发环境(JavaScript)

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

作者头像
shaonbean
发布2018-01-02 10:35:48
1.5K0
发布2018-01-02 10:35:48
举报
文章被收录于专栏:运维前线运维前线

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

安装ServerSide JavaScript环境“Node.js”

  • 安装Node.js和包管理工具npm
代码语言:javascript
复制
[root@linuxprobe ~]# yum -y install epel-release
[root@linuxprobe ~]# yum --enablerepo=epel -y install nodejs npm
  • 创建一个测试工具
代码语言:javascript
复制
[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创建测试
代码语言:javascript
复制
[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
这里写图片描述
这里写图片描述

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

  • 可以从CentOS SCLo软件存贮库进行安装
代码语言:javascript
复制
# install from SCLo
[root@linuxprobe ~]# yum --enablerepo=centos-sclo-rh -y install rh-nodejs4
  • 设置环境变量
代码语言:javascript
复制
# 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
代码语言:javascript
复制
[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'`"
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • CentOS 7 配置JS语言开发环境(JavaScript)
    • 安装ServerSide JavaScript环境“Node.js”
      • 安装ServerSide JavaScript环境Node.js 4(LTS)
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档