上一篇讲到了node可以轻松的向其他请求数据.

这一篇就来讲讲向本地服务器的数据交互.

HTTP服务器代码,s.js

 var http=require("http");
var server=http.createServer(function(req,res){
if(req.url!=="/favicon.ico"){
req.on("data",function(data){
console.log("服务器接受到的数据:"+data);
res.end();
})
}
});
server.listen(1337,"127.0.0.1",function(){
console.log("开始监听端口"+server.address().port+".....");
});

HTTP客户端代码,c.js:

 var http=require("http");
var options={
hostname:"localhost",
port:1337,
path:"/",
method:"POST"
};
var req=http.request(options);
req.write("你好");
req.end("再见.");

先运行服务器端代码,在运行客户端代码;结果是:

既然服务器可以接受客户端的代码,理所当然的是可以向客户端发送数据.

修改上面的代码,s.js:

 var http=require("http");
var server=http.createServer(function(req,res){
if(req.url!=="/favicon.ico"){
req.on("data",function(data){
console.log("服务器接受到的数据:"+data);
res.write("来自于服务器端的你好!!");
res.write("来自于服务器端的再见!!");
res.end();
});
}
});
server.listen(1337,"127.0.0.1",function(){
console.log("开始监听端口"+server.address().port+".....");
});

c.js代码:

 var http=require("http");
var options={
hostname:"localhost",
port:1337,
path:"/",
method:"POST"
};
var req=http.request(options,function(res){
res.on("data",function(chunk){
console.log("客户端接收到的数据:"+chunk);
});
});
req.write("你好");
req.end("再见.");

运行代码:

s.js:

c.js:

http.ServerResponse对象的addTrailers方法在服务器端响应数据的尾部追加一个头信息,在客户端接受到这个被追加的数据之后,可以在回调函数中通过回调函数的参数的参数值对象,即一个http.IncomingMessage对象的trailers属性获取信息.

继续修改上面的代码:

s.js

 var http=require("http");
var server=http.createServer(function(req,res){
if(req.url!=="/favicon.ico"){
req.on("data",function(data){
console.log("服务器接受到的数据:"+data);
res.write("来自于服务器端的你好!!");
res.write("来自于服务器端的再见!!");
//res.end();
});
req.on("end",function(){
res.addTrailers({"Content-MD5":"7895bf4b8828b55ceaf47747b"});
res.end();
});
}
});
server.listen(1337,"127.0.0.1",function(){
console.log("开始监听端口"+server.address().port+".....");
});

c.js

 var http=require("http");
var options={
hostname:"localhost",
port:1337,
path:"/",
method:"POST"
};
var req=http.request(options,function(res){
res.on("data",function(chunk){
console.log("客户端接收到的数据:"+chunk);
});
res.on("end",function(){
console.log("Trailer头信息:%j",res.trailers);
});
});
req.write("你好");
req.end("再见.");

运行代码:

s.js

c.js

不知道为什么客户端的信息会重复输出两遍.

有哪位大神知道的,敬请指点.

拜拜,睡觉.

最新文章

  1. 2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)(7/10)
  2. 20161005 NOIP 模拟赛 T2 解题报告
  3. listview 优化
  4. HDOJ-ACM1061(JAVA) Rightmost Digit
  5. java问题整理
  6. redis的5种数据结构的简介
  7. java基础系列——线程池
  8. swoft 源码解读【转】
  9. numpy C语言源代码调试(一)
  10. brew本地安装包
  11. Win10系列:C#应用控件进阶10
  12. python———day03
  13. 转:Java项目开发规范参考
  14. leetcode: 638.大礼包
  15. 2018.9青岛网络预选赛(H)
  16. 数据结构与算法--最短路径之Bellman算法、SPFA算法
  17. JSP隐含对象
  18. https://maven.google.com 连接不上的解决办法(转)
  19. 使用JavaScript的数组实现数据结构中的队列与堆栈
  20. 将SQL Server账户对应到Windows系统账户

热门文章

  1. New Concept English three(16)
  2. Git 配置ssh key的步骤
  3. Java多线程编程实战指南(核心篇)读书笔记(四)
  4. MPAndroidChart Wiki(译文)~Part 2
  5. css 发光样式
  6. Markdown 效果测试
  7. 21天学通C++_Day6
  8. stm32寄存器版学习笔记01 GPIO口的配置(LED、按键)
  9. spring beans 源码解读
  10. springboot 填坑一 springboot java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)