首页 > 代码库 > NodeJS+Express+Socket.io的一个简单例子

NodeJS+Express+Socket.io的一个简单例子

初始化一个NodeJS web应用:

打开CMD窗口。

运行cmd:“mkdir myapp”,新建一个文件夹,名为myapp。

运行cmd:“cd myapp”,切换到文件夹myapp。

运行cmd:“npm init”,创建文件package.json。

 

3.Express入门应用:

在上面的CMD窗口运行cmd:“npm install express --save”, 安装“express” node_module, "--save"表示保存express到package.json。

添加文件app.js到myapp文件夹,假设package.json里面的main是app.js。

复制代码
//app.js

var app = require(‘express‘)();
var http = require(‘http‘).Server(app);

app.get(‘/‘, function(req, res){
  res.send(‘<h1>Hello world</h1>‘);
});

http.listen(3000, function(){
  console.log(‘listening on *:3000‘);
});
复制代码

运行cmd:“node app”,启动myapp。

在Chrome里打开“http://localhost:3000/”,显示“Hello world”。

在CMD窗口里输入“Ctrl+C”,可终止myapp的运行。

 

4.Socket.io入门应用

在上面的CMD窗口运行:“npm install --save socket.io”。

添加index.html到myapp文件夹,内容如下:

复制代码
<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
    
  <body>  
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
      
    <script src="/socket.io/socket.io.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      var socket = io();
      $(form).submit(function(){
        socket.emit(chat message, $(#m).val());
        $(#m).val(‘‘);
        return false;
      });
    
      socket.on(chat message, function(msg){
        $(#messages).append($(<li>).text(msg));
      });
    </script>
  </body>
</html>
复制代码

修改app.js如下:

复制代码
//app.js

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

app.get(‘/‘, function(req, res){
  res.sendfile(‘index.html‘);
});

io.on(‘connection‘, function(socket){
  console.log(‘a user connected‘);
    
  socket.on(‘chat message‘, function(msg){
    console.log(‘message: ‘ + msg);

    io.emit(‘chat message‘, msg);
  });

});

app.set(‘port‘, process.env.PORT || 3000);

var server = http.listen(app.get(‘port‘), function() {
  console.log(‘start at port:‘ + server.address().port);
});
复制代码

再次运行“node index”,用Chrome打开“http://localhost:3000/”,显示如下“

 

引用:

http://www.wenjuan.com/s/MrmyYv
http://www.wenjuan.com/s/qUFzyi
http://www.wenjuan.com/s/MrmyYv
http://www.wenjuan.com/s/AVfUBfe
http://www.wenjuan.com/s/ZVfm2iF
http://www.wenjuan.com/s/QBV7Nf
http://www.wenjuan.com/s/2U3Q3u
http://www.wenjuan.com/s/3meE3y
http://www.wenjuan.com/s/M3Qr6fb
http://www.wenjuan.com/s/AZnuya
http://www.wenjuan.com/s/A77ZRz
http://www.wenjuan.com/s/bEVfIb
http://www.wenjuan.com/s/AbAzAjS
http://www.wenjuan.com/s/v6zY3m
http://www.wenjuan.com/s/ENBBZb
http://www.wenjuan.com/s/3mMbQf
http://www.wenjuan.com/s/MvMfMn
http://www.wenjuan.com/s/iAJZJj
http://www.wenjuan.com/s/n67zYj
http://www.wenjuan.com/s/3Qj6Fz
http://www.wenjuan.com/s/viimYv
http://www.wenjuan.com/s/Qrqm6b
http://www.wenjuan.com/s/JFNR32
http://www.wenjuan.com/s/Njemmi
http://www.wenjuan.com/s/aMNnIb
http://www.wenjuan.com/s/jMRrYz
http://www.wenjuan.com/s/VJJFrq
http://www.wenjuan.com/s/QVBjIne
http://www.wenjuan.com/s/yamU3y
http://www.wenjuan.com/s/ey2A7j
http://www.wenjuan.com/s/INFJBf
http://www.wenjuan.com/s/RBZJru
http://www.wenjuan.com/s/7z2YZf
http://www.wenjuan.com/s/UfyMba
http://www.wenjuan.com/s/B3IN3i
http://www.wenjuan.com/s/AbA7Zb
http://www.wenjuan.com/s/QbaiQf
http://www.wenjuan.com/s/z2mmIv
http://www.wenjuan.com/s/EnUbYn
http://www.wenjuan.com/s/eQfYNr
http://www.wenjuan.com/s/A7nAZn
http://www.wenjuan.com/s/yAviqe
http://www.wenjuan.com/s/e2YrIr
http://www.wenjuan.com/s/A7nAZn
http://www.wenjuan.com/s/yAviqe
http://www.wenjuan.com/s/YRb6R3
http://www.wenjuan.com/s/YrquQj
http://www.wenjuan.com/s/UJbIRvP
http://www.wenjuan.com/s/r2mMri
http://www.wenjuan.com/s/aaqUru
http://www.wenjuan.com/s/YnmYN3
http://www.wenjuan.com/s/fqUree
http://www.wenjuan.com/s/v6neyi
http://www.wenjuan.com/s/Vjyiii
http://www.wenjuan.com/s/f6JzEv
http://www.wenjuan.com/s/Rb6Fby
http://www.wenjuan.com/s/JZNvIj
http://www.wenjuan.com/s/vumErq
http://www.wenjuan.com/s/NjQNfmH
http://www.wenjuan.com/s/raYNJr
http://www.wenjuan.com/s/EBVRfq
http://www.wenjuan.com/s/7Z3m6b
http://www.wenjuan.com/s/JB7bEj
http://www.wenjuan.com/s/q6ZRJ3
http://www.wenjuan.com/s/yQRjaq
http://www.wenjuan.com/s/me6fqe
http://www.wenjuan.com/s/Y7nIzu
http://www.wenjuan.com/s/YZfaQn
http://www.wenjuan.com/s/JRfQNz
http://www.wenjuan.com/s/RVjU7r
http://www.wenjuan.com/s/InqU3q
http://www.wenjuan.com/s/673qui
http://www.wenjuan.com/s/FJvQzi
http://www.wenjuan.com/s/7nIVVn
http://www.wenjuan.com/s/VJJJVr
http://www.wenjuan.com/s/V32eQr
http://www.wenjuan.com/s/eYNzm2
http://www.wenjuan.com/s/FfYrQv
http://www.wenjuan.com/s/VnIJNj
http://www.wenjuan.com/s/qUfYNv
http://www.wenjuan.com/s/vA3MVv
http://www.wenjuan.com/s/6jMNrm
http://www.wenjuan.com/s/uqiuae
http://www.wenjuan.com/s/jAfiqy
http://www.wenjuan.com/s/MfeiEn
http://www.wenjuan.com/s/BRb2Mr
http://www.wenjuan.com/s/m6nAju
http://www.wenjuan.com/s/VZvUBv
http://www.wenjuan.com/s/6JniEv
http://www.wenjuan.com/s/VBnmym
http://www.wenjuan.com/s/vy2ae2
http://www.wenjuan.com/s/e2mMrq
http://www.wenjuan.com/s/6B732y
http://www.wenjuan.com/s/UFvEbu
http://www.wenjuan.com/s/rq6vya
http://www.wenjuan.com/s/mqaeY3
http://www.wenjuan.com/s/f2eUfq
http://www.wenjuan.com/s/FBBbme
http://www.wenjuan.com/s/uQVreq3
http://www.wenjuan.com/s/ieIZZb
http://www.wenjuan.com/s/jqIbai
http://www.wenjuan.com/s/NBNvAv
http://www.wenjuan.com/s/qMr6jm
http://www.wenjuan.com/s/n2YrQb
http://www.wenjuan.com/s/yAfYRn
http://www.wenjuan.com/s/nQFVn2
http://www.wenjuan.com/s/UzIV7f
http://www.wenjuan.com/s/JVZ3uq
http://www.wenjuan.com/s/UvaAZv
http://www.wenjuan.com/s/f2eUfq
http://www.wenjuan.com/s/IrQBniM
http://www.wenjuan.com/s/mqaeY3
http://www.wenjuan.com/s/rq6vya
http://www.wenjuan.com/s/UFvEbu
http://www.wenjuan.com/s/r6VjM3
http://www.wenjuan.com/s/UBJ7Fz
http://www.wenjuan.com/s/zi2i22
http://www.wenjuan.com/s/mIRfAj
http://www.wenjuan.com/s/67RbU3
http://www.wenjuan.com/s/Ina2me
http://www.wenjuan.com/s/z6byMj
http://www.wenjuan.com/s/I3IZF3
http://www.wenjuan.com/s/BjqeiyO
http://www.wenjuan.com/s/RNfUNnE
http://www.wenjuan.com/s/RNfUNnE
http://www.wenjuan.com/s/7Rj6ne
http://www.wenjuan.com/s/FnA7fu
http://www.wenjuan.com/s/NJjEVn
http://www.wenjuan.com/s/6nAJzq
http://www.wenjuan.com/s/jIjqY3
http://www.wenjuan.com/s/jieaye
http://www.wenjuan.com/s/RvmIju
http://www.wenjuan.com/s/JJzaUz
http://www.wenjuan.com/s/fQZ3ya
http://www.wenjuan.com/s/NnqMjm
http://www.wenjuan.com/s/auARzy
http://www.wenjuan.com/s/vaEFJ3
http://www.wenjuan.com/s/vaEFJ3
http://www.wenjuan.com/s/AnuMVfR
http://www.wenjuan.com/s/7vaya2S
http://www.wenjuan.com/s/q6Z3Qb
http://www.wenjuan.com/s/AnMnMz
http://www.wenjuan.com/s/A3IrQr
hthttp://www.wenjuan.com/s/AV73qi
http://www.wenjuan.com/s/QRjmUn
http://www.wenjuan.com/s/vUR32e
http://www.wenjuan.com/s/Y7Rbii
http://www.wenjuan.com/s/2umyqm
http://www.wenjuan.com/s/rIBZvq
http://www.wenjuan.com/s/eemA3i
http://www.wenjuan.com/s/EVBV3i
http://www.wenjuan.com/s/3EbI7zL
http://www.wenjuan.com/s/mYriAf
http://www.wenjuan.com/s/eYNviqb
http://www.wenjuan.com/s/fQ3Qzu
http://www.wenjuan.com/s/eUBN3e
http://c.tieba.baidu.com/p/3373993333
http://c.tieba.baidu.com/p/3372926228
http://www.wenjuan.com/s/2YvYf2
http://www.wenjuan.com/s/r6zMzi
http://www.wenjuan.com/s/7fu2AfU
http://www.wenjuan.com/s/rUneiy
http://www.wenjuan.com/s/7Fre6z
http://www.wenjuan.com/s/fyaIva
http://www.wenjuan.com/s/RZFnEz
http://www.wenjuan.com/s/ia6rYz
http://www.wenjuan.com/s/jimURr
http://www.wenjuan.com/s/YbARfe
http://www.wenjuan.com/s/Iz67jq
http://www.wenjuan.com/s/F7n632
http://www.wenjuan.com/s/NbIZVj
http://www.wenjuan.com/s/6BjEbu
http://www.wenjuan.com/s/vuqUjy
http://www.wenjuan.com/s/YbaiIr
http://www.wenjuan.com/s/3aquEb
http://www.wenjuan.com/s/Nf2UNb
http://www.wenjuan.com/s/YNFNJz
http://www.wenjuan.com/s/n6j6R3
http://www.wenjuan.com/s/aUz2Ib
http://www.wenjuan.com/s/quqm6b
http://www.wenjuan.com/s/vMnqay
http://www.wenjuan.com/s/YVFVvm
http://www.wenjuan.com/s/buEzqq

NodeJS+Express+Socket.io的一个简单例子