Node.js 中的重要API:TCP

2019-12-16

20:12:38

 

 

 

 

 

 

 

 

 

 

 

 

 

 

require('http').createServer(function(req,res){
    res.writeHead(200,{'Content-Type':'text/html'});
    res.end('<h1>Hello World</h1>');
}).listen(3000);

 

 

 

 

 

/**
 * Module dependencies.
 */

var net = require('net')

/**
 * Create server.
 */

var server = net.createServer(function (conn) {
  // handle connection
  console.log('\033[90m   new connection!\033[39m');
});

/**
 * Listen.
 */

server.listen(3000, function () {
  console.log('\033[96m   server listening on *:3000\033[39m');
});

 

 

 

 

 

 

 

/**
 * Module dependencies.
 */

var net = require('net')

/**
 * Keep track of connections.
 */

var count = 0;

/**
 * Create server.
 */

var server = net.createServer(function (conn) {
  conn.write(
      '\n > welcome to \033[92mnode-chat\033[39m!'
    + '\n > ' + count + ' other people are connected at this time.'
    + '\n > please write your name and press enter: '
  );
  count++;
  conn.on('close', function () {
    count--;
  });
});

/**
 * Listen.
 */

server.listen(3000, function () {
  console.log('\033[96m   server listening on *:3000\033[39m');
});

 

 

 

 

 

 

 

 

 

posted @ 2019-12-16 21:04  JasonPeng1  阅读(206)  评论(0编辑  收藏  举报