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

前端之家收集整理的这篇文章主要介绍了CentOS 7 配置JS语言开发环境(JavaScript)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

安装ServerSide JavaScript环境“Node.js”

  • 安装Node.js和包管理工具npm
  1. [root@linuxprobe ~]# yum -y install epel-release
  2. [root@linuxprobe ~]# yum --enablerepo=epel -y install nodejs npm
  • 创建一个测试工具
  1. [root@linuxprobe ~]$ vi helloworld.js
  2. var http = require('http');
  3. http.createServer(function (req,res) {
  4. res.writeHead(200,{'Content-Type': 'text/plain'});
  5. res.end('Hello World\n');
  6. }).listen(1337,'127.0.0.1');
  7. console.log('listening on http://127.0.0.1:1337/');
  8.  
  9. # run server
  10. [root@linuxprobe ~]$ node helloworld.js &
  11. # verify (it's OK if following reply is back )
  12. [root@linuxprobe ~]$ curl http://127.0.0.1:1337/
  13. Hello World
  • 安装Socket.IO并使用WebSocket创建测试
  1. [root@linuxprobe ~]$ npm install socket.io express
  2. [root@linuxprobe ~]$ vi chat.js
  3. var app = require('express')();
  4. var http = require('http').Server(app);
  5. var io = require('socket.io')(http);
  6.  
  7. app.get('/',function(req,res){
  8. res.sendFile(__dirname + '/index.html');
  9. });
  10.  
  11. io.on('connection',function(socket){
  12. socket.on('chat message',function(msg){
  13. io.emit('chat message',msg);
  14. });
  15. });
  16.  
  17. http.listen(1337,function(){
  18. console.log('listening on *:1337');
  19. });
  20.  
  21. [root@linuxprobe ~]$ vi index.html
  22. <!DOCTYPE html>
  23. <html>
  24. <head>
  25. <title>WebSocket Chat</title>
  26. </head>
  27. <body>
  28. <form action="">
  29. <input id="sendmsg" autocomplete="off" /><button>Send</button>
  30. </form>
  31. <ul id="messages" style="list-style-type: decimal; font-size: 16px; font-family: Arial;"></ul>
  32. <script src="/socket.io/socket.io.js"></script>
  33. <script src="http://code.jquery.com/jquery.min.js"></script>
  34. <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>
  35. </body>
  36. </html>
  37.  
  38. [root@linuxprobe ~]$ node chat.js
  39. listening on *:1337

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

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

猜你在找的CentOS相关文章