网络分层
而在实际操作系统中,只有应用层、传输层、网络层,链路层。
Node提供了net、dgram、http、https四个模块,分别处理TCP、UDP、HTTP、HTTPS.适用于服务器和客户端。
TCP
httpcode
code | 说明 |
---|---|
200 | 请求被成功处理 |
301/302 | 永久性重定向/临时性重定向 |
403 | 没有权限访问 |
404 | 表示没有对应资源 |
401(未授权) | 请求要求身份验证。对于登录后请求的网页,服务器可能返回此响应(如知乎这种需要登录才能查看的) |
500 | 服务器错误 |
503 | 服务器停机或正在维护 |
传输控制协议
在OSI模型(物理层、数据链结层、网络层、传输层、会话层、表示层、应用层)中属于传输层协议
TCP是面向连接的协议,在传输之前需要三次握手形成会话。
戳这里看详解
创建TCP服务器
var net = require('net');
var server = net.createServer( function (socket) {
//新的连接
socket.on('data', function (data) {
socket.write("您好")
});
socket.on('end', function () {
console.log('连接断开')
});
socket.write("实例: \n");
});
server.listen(8124, function () {
console.log('server bound');
});
通过net模块自行构造客户端进行会话,测试上面构建的TCP服务的代码如下:
var net = require('net');
var client = net.connect({port:8124}, function () {
//'connect' listener
console.log('client connected');
client.write('world !\r\n');
});
client.on('data', function (data) {
console.log(data.String());
client.end();
});
client.on('end', function () {
console.log('client disconnected');
});
以上客户端代码存为client.js并执行
$ node client.js
client connected
实例:
您好
client disconnected
UDP
用户数据包协议,与TCP一样同属于网络传输层。UDP不是面向连接的。DNS服务基于它实现。
创建UDP服务端
var dgram = require ('dgram');
var server = dgram.createSocket("udp4");
server.on ("message", function (msg, rinfo) {
console.log("server got:" + msg + "from" + rinfo.address + ":" + rinfo.port);
});
server.on("listening", function () {
var address =server.address();
console.log("server listening" + address.address + ":" + address.port);
});
server.bind(41234);
创建UDP客户端
var dgram = require('dgram');
var message = new Buffer ("Node.js")
var client = dgram.createSocket("udp4");
client.send(message, 0, message.length, 41234, "localhost", function (err, bytes) {
client.close();
});
保存为client.js并执行,服务器端的命令行将会如下输出
$ node client.js
server listening 0.0.0.0:41234
server got: Node.js from 127.0.0.1:58682
与TCP套接字的Write()相比,send()方法的参数列表相对复杂,但是它更灵活的地方在于可以随意发送数据到网络中的服务器端,而TCP如果要发送数据给另一个服务器端,则需要重新通过套接字构造新的连接。
HTTP
TCP与UDP都是属于网络传输层协议,如果要构建高效的网络应用,就应该从传输层进行着手。
Node提供了基本的http和https模块用于HTTP和HTTPS的封装,对于其他应用层协议(SMTP、IMAP等等)也能从社区找到实现。
实现HTTP服务器:
var http = require('http');
http.createServer( function (req, res) {
res.wirteHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337,'127.0.0.1');
console.log("Server running at http://127.0.0.1:1337/ ")
尽管这个小服务器只能回复Hello World,但是它能维持并发量和QPS都是可观的。
HTTP报文
HTTP全称超文本传输协议,它构建在TCP之上属于应用层协议,HTTP的两端是服务器和浏览器(B/S模式)
http模块
关于http模块的一些实例 可以戳这里
Serving HTML Page
var http = require('http');
var fs = require('fs');
var server = http.createServer(function(req, res) {
console.log('request was made: ' + req.url);
res.writeHead(200,{'Content-Type': 'text/html'});
var myReadStream = fs.createReadStream(__dirname + '/index.html','utf8');
myReadStream.pipe(res);
});
server.listen(3000,'127.0.0.1');
console.log('now listening to port 3000');
Serving JSON data
ar http = require('http');
var fs = require('fs');
var server = http.createServer(function(req, res) {
console.log('request was made: ' + req.url);
res.writeHead(200,{'Content-Type': 'application/json'});
var myObj = {
name: 'AlexZ33',
job: "Engineer",
age: '26'
}
res.end(JSON.stringify(myObj)); //end()括号内是string 或者buffer,所以我们不能直接用myObj这个对象,要转换
});
server.listen(3000,'127.0.0.1');
console.log('now listening to port 3000');
HTTPS 和 HTTP2
net.Socket
net.Socket 是一个双工的流(duplex stream)
// net.d.ts
class Socket extends stream.Duplex {
constructor(options?: SocketConstructorOpts);
// Extended base methods
write(buffer: Buffer | Uint8Array | string, cb?: (err?: Error) => void): boolean;
write(str: Buffer | Uint8Array | string, encoding?: string, cb?: (err?: Error) => void): boolean;
connect(options: SocketConnectOpts, connectionListener?: () => void): this;
connect(port: number, host: string, connectionListener?: () => void): this;
connect(port: number, connectionListener?: () => void): this;
connect(path: string, connectionListener?: () => void): this;
setEncoding(encoding?: string): this;
pause(): this;
resume(): this;
setTimeout(timeout: number, callback?: () => void): this;
setNoDelay(noDelay?: boolean): this;
setKeepAlive(enable?: boolean, initialDelay?: number): this;
address(): AddressInfo | string;
unref(): void;
ref(): void;
readonly bufferSize: number;
readonly bytesRead: number;
readonly bytesWritten: number;
readonly connecting: boolean;
readonly destroyed: boolean;
readonly localAddress: string;
readonly localPort: number;
readonly remoteAddress?: string;
readonly remoteFamily?: string;
readonly remotePort?: number;
// Extended base methods
end(cb?: () => void): void;
end(buffer: Buffer | Uint8Array | string, cb?: () => void): void;
end(str: Buffer | Uint8Array | string, encoding?: string, cb?: () => void): void;
/**
* events.EventEmitter
* 1. close
* 2. connect
* 3. data
* 4. drain
* 5. end
* 6. error
* 7. lookup
* 8. timeout
*/
addListener(event: string, listener: (...args: any[]) => void): this;
addListener(event: "close", listener: (had_error: boolean) => void): this;
addListener(event: "connect", listener: () => void): this;
addListener(event: "data", listener: (data: Buffer) => void): this;
addListener(event: "drain", listener: () => void): this;
addListener(event: "end", listener: () => void): this;
addListener(event: "error", listener: (err: Error) => void): this;
addListener(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
addListener(event: "timeout", listener: () => void): this;
emit(event: string | symbol, ...args: any[]): boolean;
emit(event: "close", had_error: boolean): boolean;
emit(event: "connect"): boolean;
emit(event: "data", data: Buffer): boolean;
emit(event: "drain"): boolean;
emit(event: "end"): boolean;
emit(event: "error", err: Error): boolean;
emit(event: "lookup", err: Error, address: string, family: string | number, host: string): boolean;
emit(event: "timeout"): boolean;
on(event: string, listener: (...args: any[]) => void): this;
on(event: "close", listener: (had_error: boolean) => void): this;
on(event: "connect", listener: () => void): this;
on(event: "data", listener: (data: Buffer) => void): this;
on(event: "drain", listener: () => void): this;
on(event: "end", listener: () => void): this;
on(event: "error", listener: (err: Error) => void): this;
on(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
on(event: "timeout", listener: () => void): this;
once(event: string, listener: (...args: any[]) => void): this;
once(event: "close", listener: (had_error: boolean) => void): this;
once(event: "connect", listener: () => void): this;
once(event: "data", listener: (data: Buffer) => void): this;
once(event: "drain", listener: () => void): this;
once(event: "end", listener: () => void): this;
once(event: "error", listener: (err: Error) => void): this;
once(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
once(event: "timeout", listener: () => void): this;
prependListener(event: string, listener: (...args: any[]) => void): this;
prependListener(event: "close", listener: (had_error: boolean) => void): this;
prependListener(event: "connect", listener: () => void): this;
prependListener(event: "data", listener: (data: Buffer) => void): this;
prependListener(event: "drain", listener: () => void): this;
prependListener(event: "end", listener: () => void): this;
prependListener(event: "error", listener: (err: Error) => void): this;
prependListener(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
prependListener(event: "timeout", listener: () => void): this;
prependOnceListener(event: string, listener: (...args: any[]) => void): this;
prependOnceListener(event: "close", listener: (had_error: boolean) => void): this;
prependOnceListener(event: "connect", listener: () => void): this;
prependOnceListener(event: "data", listener: (data: Buffer) => void): this;
prependOnceListener(event: "drain", listener: () => void): this;
prependOnceListener(event: "end", listener: () => void): this;
prependOnceListener(event: "error", listener: (err: Error) => void): this;
prependOnceListener(event: "lookup", listener: (err: Error, address: string, family: string | number, host: string) => void): this;
prependOnceListener(event: "timeout", listener: () => void): this;
}
net.Server
参考
Node.js知识点详解(二)HTTP模块与事件模块
Anatomy of an HTTP Transaction
《nodejs 深入浅出》
《图解HTTP》
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。