前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CentOS 7.2 部署Node.js开发环境

CentOS 7.2 部署Node.js开发环境

作者头像
shaonbean
发布2019-05-26 09:44:00
8780
发布2019-05-26 09:44:00
举报
文章被收录于专栏:运维前线运维前线

版权声明:本文为木偶人shaon原创文章,转载请注明原文地址,非常感谢。 https://cloud.tencent.com/developer/article/1434734

一、Node.js简介

  • Node.js是一个Javascript运行环境(runtime)。实际上它是对Google V8引擎进行了封装。V8引 擎执行Javascript的速度非常快,性能非常好。Node.js对一些特殊用例进行了优化,提供了替代的API,使得V8在非浏览器环境下运行得更好。
  • Node.js是一个基于Chrome JavaScript运行时建立的平台,用于方便地搭建响应速度快、易于扩展的网络应用。Node.js 使用事件驱动, 非阻塞I/O模型而得以轻量和高效,非常适合在分布式设备上运行数据密集型的实时应用。
  • Node.js中文网:http://nodejs.cn/
  • Node.js API文档:http://nodeapi.ucdok.com/#/api/
  • Node.js菜鸟教程:http://www.runoob.com/nodejs/nodejs-tutorial.html

二、Node.js安装

  • 安装Node.js和npm,包管理工具
代码语言:javascript
复制
[root@linuxprobe ~]# yum --enablerepo=epel -y install nodejs npm
  • 以普通用户(wang)创建测试工具
代码语言:javascript
复制
[wang@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
[wang@linuxprobe ~]$ node helloworld.js &
[1] 12440
[wang@linuxprobe ~]$ listening on http://127.0.0.1:1337/
# verify (it's OK if following reply is back )
[wang@linuxprobe ~]$ curl http://127.0.0.1:1337/
Hello World 
  • 安装Socket.IO并使用WebSocket创建测试
代码语言:javascript
复制
[wang@linuxprobe ~]$ npm install socket.io express
# 安装express会提示下图警告,经度娘了解,这个警告信息可以忽略,本文只介绍安装Node.js环境,对Node.js本身不做过多介绍,有兴趣的同学可以想办法解决这个WARN。
代码语言:javascript
复制
[wang@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');
});

[wang@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>

[wang@linuxprobe ~]$ node chat.js
listening on *:1337 

从客户端计算机访问“http://(服务器的主机名或IP地址):1337 /”,以确保示例应用程序正常工作

  • 源码安装Node.js,CentOS 7.2 minimal
代码语言:javascript
复制
# 安装开发依赖包
[root@linuxprobe ~]# yum -y install gcc make gcc-c++ openssl-devel wget
# 下载源码及解压
[root@linuxprobe ~]# wget https://nodejs.org/dist/v6.2.0/node-v6.2.0-linux-x64.tar.gz -P /usr/local/src
[root@linuxprobe ~]# cd /usr/local/src && tar zxvf node-v6.2.0-linux-x64.tar.gz
# 编译安装
[root@linuxprobe src]# cd node-v6.2.0-linux-x64
# 编译安装是个坑,先不填,放着......
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年11月04日,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、Node.js简介
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档