nodejs提供了net,http,dgram模块,分别实现了TCP,HTTP,UDP的通讯
OSI参考模型将网络通信功能分成了7层,即物理层,数据链路层,网络层,传输层,会话层,表示层和应用层。而TCP就是传输层的协议。
const net = require ('net');
const port = 80;
const server = net.createServer(function (socket){
console.log('net conntect');
// 设置最大连接数
server.maxConnections = 2;
server.getConnections(function (err,count){
console.log(`howmuch ${count}`);
// 获取地址信息
var address = server.address();
var message = `client, the server address is ${JSON.stringify(address)}`
socket.write(message, function (){
var writeSize = socket.bytesWritten;
console.log(message+ 'has send');
console.log(socket);
})
server.on('data',function (data){
console.log(data.toString());
})
})
})
server.listen(port, function (){
console.log('server is listen',port)
})
同样是使用net模块
const net = require ('net');
const port = 80;
const client = net.Socket();
client.connect(port,'localhost', function (socket){
console.log('net conntect');
client.write('message from client')
})
client.on('data',function (data){
console.log(`the data is from `, data.toString());
})
client.on('end',function (data){
console.log(`data end `);
})
nodejs 端 文件
const http = require ('http');
const fs = require ('fs');
const port = 80;
const server = http.createServer(function (req,res){
res.writeHead(200, {
'content-type': 'text/js'
})
const data = fs.readFileSync('./index.html');
res.write(data);
res.end();
})
server.listen(port,function (){
console.log(port + ' port is listenning');
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>test</title>
</head>
<body>
<h1>
hi i am nodejs
</h1>
</body>
</html>
module.defaults = {
".html": 'text/html',
".css": 'text/css',
".js": 'text/javascript',
".gif": 'image/gif',
".png": 'image/png',
".jpg": 'image/jpg',
".jpeg": 'image/jpeg',
".ico": 'image/x-icon',
}
const http = require ('http');
const fs = require ('fs');
const url = require ('url');
const path = require ('path');
const mime = require ('./mime');
const port = 80;
const server = http.createServer(function (req,res){
let filePath = `.${req.url}`;
fs.stat(filePath, function (err, stats) {
if (err) {
return console.error(err);
}
// 检测文件类型
console.log("是否为文件(isFile) ? " + stats.isFile());
console.log("是否为目录(isDirectory) ? " + stats.isDirectory());
if(stats.isFile()){
fs.exists(filePath,function (isExist){
if(isExist){
console.log("读取文件信息成功!");
const type = path.parse(filePath).ext;
const contentType = mime[type];
const data = fs.readFileSync(filePath)
res.writeHead(200,{
'content-type': contentType
})
res.write(data);
res.end();
}else{
res.end('404 not found');
}
})
} else {
fs.readdir(filePath, function(err, files){
if (err) {
return console.error(err);
}
let html = '';
files.forEach( function (file){
html += `
<h3>
<a href="${filePath}/${file}">${file}</a>
</h3>
`;
});
res.write(html);
res.end();
});
}
});
})
server.listen(port,function (){
console.log(`${port} is listenning`)
})
const dgram = require ('dgram');
const message = new Buffer('some message from server');
const socket = dgram.createSocket('udp4', function (msg, rinfo){
console.log(msg.toString());
socket.send(message,0,message.length,rinfo.port,rinfo.address, function (err, bytes){
if(err){
console.log(err);
return;
}
console.log(`send ${bytes} message`);
})
});
socket.bind(80, 'localhost', function (){
console.log('bind 80 port');
})
const dgram = require ('dgram');
const message = new Buffer('some message from client');
const socket = dgram.createSocket('udp4',)
socket.send(message,0,message.length,80,'localhost' , function (msg, rinfo){
console.log(msg);
socket.send(message, 0 , message.length,80, rinfo.port, rinfo.address, function (err){
if(err){
console.log(err);
return;
}
console.log(`send ${bytes} message`)
})
})
socket.on('message', function (){
console.log(`some message form server`);
})
// 基础选择器
$('*') 选择所有
$('div , span') 选择所有span,div
// 层次选择器
$('parent child') // 选择parent所有child的后代元素
$('parent > child') // 选择parent下的子元素
$('parent + child') // 选择parent的后面的同辈子元素
$('parent ~ child') // 选择parent的后面的所有同辈子元素
// 基本过滤器
:first 选择第一个子元素
:last 选择最后一个子元素
:not(selector): 除去所有给定选择器所匹配的元素,返回集合元素 //$$('*:not(div):not(span):not(a):not(input):not(meta):not(link):not(li):not(svg)')
:even 偶数选择
:odd 奇数选择器
:ep(index) 选择索引等于第几个元素
:gt(index) 选择索引大于index的所有元素
:lt(index) 选择索引小于index的所有元素
:header 选择所有的标题元素,返回集合
:animated: 选择正在执行动画的元素
:focus
// 内容过滤选择器
:empty 选择没有子节点的元素
// 子元素过滤器
:nth-child(index/even/odd/3n) // 选第几个元素
:first-child
:last-child
:only-child