The Node HTTP server is built on top of the Node TCP server, which means http. server inherits from net. serverCopy the code

First let’s write a simple server in Node.js

const http=require('http');
const server=http.createServer(function(req,res){
    res.writeHead(200, {'Content-Type':'text/html'})
    res.end('<h1>hello</h1>')
    console.log("new connection!")
})
server.listen(port=3001, hostname='localhost'.() = > {
    console.log(`server running at http://${hostname}:${port}/ `);
  });
  
Copy the code

The server outputs its port and host name when it is started, and “New Connection” on the console when a client is connected.

Test with Telnet

The Telnet command uses THE TCP protocol and is generally used for remote login (plaintext transmission is insecure and has been replaced by SSH), as well as to check the connectivity of a specified port. Ping is based on ICMP, which can be disabled.

Telnet is really too old, in WIN groped for a long time to know his usage, at first I thought I installed wrong

1. Enable the Telnet client service

In the Control panel, click open program, choose to enable or disable Windows services, select Telnet client, and restart the computer.

2. Test the connectivity of the specified port

Enter Telnet localhost 3001 to enter the black screen, CTRL +] to enter the Telnet command line, then press Enter to enter the black screen, enter GET/HTTP/1.1 to create an HTTP request, That is, a socket (bound to the destination IP address of communication, transport layer protocol (TCP or UDP) used, and port number used), and then press Enter twice to obtain a response packet.

Second, based on TCP chat procedures

1. Client interaction

On Windows, the carriage return newline symbol is “\r\n”. On Mac, each line ends with “< carriage return >”, which is “\r”, but on Linux and other systems there is no “\r” symbol.

const net=require('net');
var count=0
const server=net.createServer(function(conn){
    conn.write('> hello welcome! '+'\r\n'+
    '>'+count+' other people are connected at this time'+
    '\r\n'+'> please write your name and enter');
    count++
})
server.listen(port=3001, hostname='localhost'.() = > {
    console.log(`server running at http://${hostname}:${port}/ `);
  });
Copy the code

2. Set the nickname and advertise the message

const net=require('net');
var count=0
,users={}
const server=net.createServer(function(conn){
    conn.write('> hello welcome! '+'\r\n'+
    '>'+count+' other people are connected at this time'+
    '\r\n'+'> please write your name and enter: ');
    count++;
    var nickname;
    conn.setEncoding('utf8')
    conn.on('data'.function(data){
        console.log(data)
        //data=data.replace('\r\n','')
        if(! nickname){if(users[data]){
                conn.write('\033[93m> nickname already in use. try again \033[39m\r\n')
                return
            }else{
                conn.write('\r\n')
                nickname=data;
                users[nickname]=conn
                for(var i in users){
                    users[i].write('\033[90m >'+nickname+'joined the room \033[39m\r\n')}}}else{
            for(var j in users){
                if(j! ==nickname){ users[j].write('\033[90m >'+nickname+' \033[39m'+data+'\r\n')
                }
            }
        }
    })
    conn.on('close'.function(){
        count--;
        //delete users[nickname]
    })
})
server.listen(port=3001, hostname='localhost'.() = > {
    console.log(`server running at http://${hostname}:${port}/ `);
  });
  
Copy the code

Originally wanted to write well, Telnet really too dog, character output, line output mode below see interactive information, irritable, this section so.