Nodejs的http模块的基础 学到的东西

Nodejs的http模块的基础 学到的东西

其中 客户端: 我们在node.js中如果要请求服务端中的js或者其他脚本的话要使用 http.request()方法他会返回http.ClientRequest这个对象的实例。而这个对象 中的第二个参数是一个函数这个函数中的参数为http.ClientReponse这个对象的实例(也就是要把服务器返回的内容回显出来的一个操作)

http.request(options,function(**res**){
  res.setEncoding('utf8')  设置请求的字编码
});

http.ClientReponse这个对象绑定data 事件他有一个参数也就是chunk这个是服务器回显示给客户端的内容

  res.on('data',function(**chunk**){
      console.log(**chunk**)   这里也就是服务器返回来的内容
  })

注意:在定完http.request() 后一定要记得req.end()使用这个方法,因为如果不使用这个方法那么他会一直请求(一直等待)。还有要发送(我里我理解成发送也就是http.request()这个对象中的write()方法)

把POST或者GET的内容发送给指定的页面中。

服务器: 在服务端接收方面来说: 使用http.Server()方法。再绑定request这个事件。他会返回有两个实例一个是

var http = require('http')
var server = http.Server();

server.on(‘request’,function(req,res){ req.on(‘data’,function(chunk){ //这里的 chunk 就是客户端表单提交过来的东西 res.write(内容)要回显示给客户端的内容 在客户端页面的http.ClientRepsonse这个对象负责接收这个信息 }) })

http.ServerRequest req客户端请求的内容都在这个对象中 和 http.Reponse res 要发送给客户端的属性和方法都在这个对象中 也就是这个函数第二个参数中的两个参数

其中:http.ServerRequest对象中有data事件,data事件的中有一个参数chunk这个参数就是 服务器所接收到的内容

呵今天就搞懂这么多呵,第一次写这个希望对大家有帮助 文笔不怎么样呵 如果要不好的地方希望大家多多包含


4 回复

Node.js 的 http 模块基础 学到的东西

客户端请求

在 Node.js 中,如果你想从服务器请求数据或执行一些操作,可以使用 http.request() 方法。该方法会返回一个 http.ClientRequest 对象的实例。通过监听这个对象上的事件,我们可以处理服务器的响应。

示例代码:

const http = require('http');

// 请求配置
const options = {
  hostname: 'www.example.com',
  port: 80,
  path: '/path/to/resource',
  method: 'GET'
};

// 发起请求
const req = http.request(options, (res) => {
  // 设置响应的数据编码格式
  res.setEncoding('utf8');
  
  // 监听数据事件,获取服务器返回的内容
  res.on('data', (chunk) => {
    console.log(chunk);  // 打印服务器返回的内容
  });
});

// 请求结束时调用 req.end()
req.end();

解释:

  • http.request() 方法用于发起 HTTP 请求。
  • 第二个参数是一个回调函数,该函数接收一个 http.ClientResponse 对象作为参数。
  • setEncoding('utf8') 方法设置响应数据的字符编码。
  • res.on('data', ...) 监听 data 事件,每次接收到服务器返回的数据片段时都会触发这个事件。chunk 是接收到的数据片段。

服务器端处理

在服务器端,我们可以通过创建一个 http.Server 实例,并监听其 request 事件来处理客户端的请求。

示例代码:

const http = require('http');

// 创建服务器实例
const server = http.createServer((req, res) => {
  // 处理客户端请求
  let body = [];
  req.on('data', (chunk) => {
    body.push(chunk);
  }).on('end', () => {
    body = Buffer.concat(body).toString();
    
    // 响应客户端
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end(`Received data: ${body}`);
  });
});

// 监听端口
server.listen(3000, () => {
  console.log('Server is running on port 3000');
});

解释:

  • http.createServer() 方法创建一个新的 HTTP 服务器。
  • createServer 接收一个回调函数,该函数接收两个参数:reqres
    • reqhttp.ServerRequest 对象,表示客户端的请求。
    • reshttp.ServerResponse 对象,用于向客户端发送响应。
  • req.on('data', ...) 监听 data 事件,处理客户端发送的数据。
  • req.on('end', ...) 监听 end 事件,当所有数据都已读取完毕时触发。
  • res.writeHead() 设置响应的状态码和头部信息。
  • res.end() 结束响应并发送数据给客户端。

以上就是 Node.js 中 http 模块的一些基本使用方法。希望这些示例代码和解释对你有所帮助!


大侠 求教一个问题http://cnodejs.org/topic/513b62c3df9e9fcc58ab2bea

呵对不起啊我也是新手 呵还没学到呢

Node.js 的 HTTP 模块基础

客户端

在 Node.js 中,如果你需要向服务器请求 JavaScript 文件或其他脚本,可以使用 http.request() 方法。这个方法会返回一个 http.ClientRequest 对象的实例。你需要监听 http.ClientResponse 对象的 data 事件来处理服务器返回的数据。

const http = require('http');

// 配置请求选项
const options = {
  hostname: 'www.example.com',
  port: 80,
  path: '/path/to/resource',
  method: 'GET'
};

// 创建请求
const req = http.request(options, (res) => {
  // 设置响应数据的编码
  res.setEncoding('utf8');

  // 处理服务器返回的数据
  res.on('data', (chunk) => {
    console.log(`Received data: ${chunk}`);
  });

  // 请求结束时触发
  res.on('end', () => {
    console.log('Data received successfully.');
  });
});

// 如果有 POST 数据,可以在这里发送
// req.write('Some data to send');

// 结束请求
req.end();

服务器端

在服务器端,你可以使用 http.createServer() 方法来创建一个 HTTP 服务器,并通过监听 request 事件来处理客户端的请求。

const http = require('http');

const server = http.createServer((req, res) => {
  // 设置响应头
  res.writeHead(200, {'Content-Type': 'text/plain'});

  // 处理 GET 或 POST 请求的数据
  let body = [];
  req.on('data', (chunk) => {
    body.push(chunk);
  }).on('end', () => {
    body = Buffer.concat(body).toString();
    console.log(`Received request: ${body}`);

    // 发送响应数据
    res.end('Hello, World!');
  });
});

// 监听端口
server.listen(3000, () => {
  console.log('Server is running on port 3000');
});

以上代码展示了如何使用 Node.js 的 HTTP 模块进行基本的客户端请求和服务器端响应。希望对你有所帮助!

回到顶部