Nodejs中http在ondata事件以后,就不会触发onrequest,反之依然
Nodejs中http在ondata事件以后,就不会触发onrequest,反之依然
JS var server=require(‘http’).createServer(); server.on(‘request’, function(req, res) { console.log(’ request '); }); server.on(“connection”,function (sock) { console.log(‘connection comming…’); /* sock.ondata=function (data) { console.log(‘data…’); console.log(data); } */ }); server.on(“close”,function (sock) { console.log(‘connection gone away…’); }); server.listen(8080);
Node.js 中 http 模块的 onrequest 和 ondata 事件
在 Node.js 的 HTTP 模块中,onrequest 和 ondata 这两个事件在处理请求时的行为有所不同。本文将通过示例代码来解释这些行为。
示例代码
var http = require('http');
// 创建一个 HTTP 服务器
var server = http.createServer();
// 监听 'request' 事件
server.on('request', function(req, res) {
console.log('Request received');
res.end('Hello, World!');
});
// 监听 'connection' 事件
server.on('connection', function(socket) {
console.log('New connection established');
// 监听 'data' 事件
socket.on('data', function(chunk) {
console.log('Data received:', chunk.toString());
});
// 监听 'end' 事件
socket.on('end', function() {
console.log('Connection ended');
});
});
// 监听 'close' 事件
server.on('close', function() {
console.log('Server closed');
});
// 监听 'error' 事件
server.on('error', function(err) {
console.error('Error occurred:', err);
});
// 启动服务器
server.listen(8080, function() {
console.log('Server listening on port 8080');
});
解释
-
request事件:- 当客户端发起一个 HTTP 请求时,
request事件会被触发。 - 在上面的代码中,当有新的请求到达时,控制台会输出 “Request received”。
request事件会传递两个参数:req(请求对象)和res(响应对象)。你可以通过res.end()方法发送响应给客户端。
- 当客户端发起一个 HTTP 请求时,
-
connection事件:- 当一个新的 TCP 连接被建立时,
connection事件会被触发。 - 在上面的代码中,当有新的连接建立时,控制台会输出 “New connection established”。
- 你可以在
socket对象上监听其他事件,例如data和end。
- 当一个新的 TCP 连接被建立时,
-
data事件:- 当从客户端接收到数据时,
data事件会被触发。 - 在上面的代码中,当接收到数据时,控制台会输出 "Data received: <data>"。
- 注意,
data事件通常在request事件之后触发,因为request事件已经处理了请求头信息。
- 当从客户端接收到数据时,
-
end事件:- 当客户端关闭连接时,
end事件会被触发。 - 在上面的代码中,当连接结束时,控制台会输出 “Connection ended”。
- 当客户端关闭连接时,
总结
request事件会在接收到完整的 HTTP 请求后触发,包括请求头和请求体。data事件会在接收到请求体的一部分数据时触发。- 一旦
request事件被触发,data事件就会开始触发,反之亦然。
因此,在实际应用中,你应该根据需要选择合适的事件来处理不同的场景。
在Node.js中,HTTP服务器处理请求时,request事件会在接收到完整的请求头之后触发,而data事件则会在接收到请求体数据时触发。这两个事件并不互相排斥,而是可以同时工作。下面是一个示例代码来说明这一点:
var http = require('http');
var server = http.createServer(function (req, res) {
console.log('Request received');
req.on('data', function (chunk) {
console.log('Data chunk received:', chunk);
});
req.on('end', function () {
console.log('Request body processing complete');
res.end('Hello World\n');
});
});
server.on('connection', function (socket) {
console.log('New connection established');
});
server.on('close', function () {
console.log('Server closed');
});
server.listen(8080, function () {
console.log('Server listening on port 8080');
});
解释
- 创建服务器:使用
http.createServer创建一个HTTP服务器。 - 处理请求:服务器使用回调函数处理每个请求,该回调函数接收两个参数:
req(请求对象)和res(响应对象)。 - 监听data事件:在请求对象上监听
data事件,每当接收到一部分请求体数据时,会打印出该部分数据。 - 监听end事件:在请求对象上监听
end事件,当整个请求体被完整接收时触发,表示请求处理完成。 - 连接和关闭事件:服务器还监听了
connection和close事件,分别用于处理新连接建立和服务器关闭的情况。
在这个示例中,无论是否触发data事件,request事件都会被触发,两者之间并没有排斥关系。你可以根据需要组合使用这些事件来处理HTTP请求。

