node下 http,https,tcp服务的创建

HTTP协议

var http = require('http');
var server = http.createServer(); // server eventEmitter

server.on('request', function (request, response) {
     
console.log('666666666666666666')
	}).listen(9080, function () {
     
	
});

TCP协议

var net = require("net");
// tcp服务端
server1 = net.createServer(function(client){
     
     // 给客户端返回数据
    client.write('Hello World!\r\n');
});
server1.listen(9001);

// telnet localhost 9001

HTTPS协议
首先生成公钥和私钥

openssl
# 生成私钥
genrsa -out id_rsa_private 2048
# 生成私钥(RSA私钥转换成PKCS8格式)
pkcs8 -topk8 -inform PEM -in id_rsa_private -outform pem -nocrypt -out id_rsa_private_pkcs
# 生成公钥
rsa -in id_rsa_private -pubout -out id_rsa_public.pub
const https = require('https');
const fs = require('fs');

const key = fs.readFileSync('./key.pem');
const cert = fs.readFileSync('./cert.pem');

const options = {
     
  key:key,
  cert:cert
};

https.createServer(options, (req, res) => {
     
  res.writeHead(200);
  res.end('hello world-----\n');
}).listen(8000);

你可能感兴趣的:(前端)