Node学习笔记2:建立HTTP服务器和客户端之间的通信

http服务器端:

var http = require('http');

var server = http.createServer();

server.on('request', function(req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/plain',
    'Content-Control':'max-age=3600'
  });
  res.write('Hello Worldddd');
  res.end(req.url);
});

server.listen(4000);

http客户端代码:

var http = require('http');
var util = require('util');
var options = {
  host:'localhost',
  port:4000,
  path:'/system.txt',
  method:'POST'
}

var request = http.request(options,function(response){
  response.setEncoding('utf8')
  response.on('data',function(data){
    console.log(data);
  })
})
request.end();

结果:

 

posted @ 2019-01-23 16:36  小猪冒泡  阅读(127)  评论(0编辑  收藏  举报