5.nodejs权威指南--HTTP

时间:2021-01-08 23:44:14

1. HTTP

1.1 服务器

var http = require('http');

var server = http.createServer(function(req,rsp){

req.on('data',function(data){

});

req.on('end',function(){

});

rsp.writeHead(200,{

'Content-Type':'text/plain',

'Access-Control-Allow-Origin':'http:127.0.0.1'

});

rsp.write('ok');

rsp.end();

});

server.listen(12345,'127.0.0.1');

1.2 客户端

var http = require('http');

var options = {

hostname:'127.0.0.1',

port:12345,

path:'/',

method:'POST'

};

var req = http.request(options);

req.write('to server');

req.end();