nodeAPI--HTTP

HTTP:   //超文本协议,是属于TCP上层的协议

  • http协议构建在请求和响应概念上,node.js中对应http.ServerRequest,http.ServerResponse;
  • 当用户浏览网站,用户代理(浏览器)会创建一个请求,该请求通过TCP发送给web服务器;

流对接:

var http = require('http');
var fs = require('fs');
var stream;

http.createServer(function (req, res) {
	res.writeHead(200,{'Content-Type':'image/png'});
	stream = fs.createReadStream('1.png');
	stream.on('data', function(data) {
		res.write(data);
	});
	stream.on('end', function() {
		res.end();
	});
}).listen(3000);
--------------------------------------------
require('http').createServer(function (req, res) {
	res.writeHead(200,{'Content-Type':'image/png'});
	require('fs').createReadStream('1.png').pipe(res);
}).listen(3000);

一个简单web服务器的例子:

  • querystring模块:它能将一个字符串解析成一个对象;
require('querystring').parse('name = guillermo');
//{"name":"guillermo"}
  • url和method判断,headers :req.url ; req.method //返回的是大写; req.headers //返回的是数组
  • data和end事件
var http = require('http');
var qs = require('querystring');
http.createServer(function(req, res) {
	if('/' === req.url) {
		res.writeHead(200, {'Content-Type': 'text/html'});
		res.end([
			'<form method="post" action="/url">',
			'<h1>My form</h1>',
			'<fieldset>',
			'<label>Personal information</label>',
			'<p>What is your name?</p>',
			'<input type = "text" name = "name">',
			'<p><button>Submit</button></p>',
			'</form>'
		].join(''));
	} else if('/url' === req.url && 'POST' === req.method) {
		var body = '';
		req.on('data', function(chunk) {
			body += chunk;
		});
		req.on('end', function() {
			res.writeHead(200, {'Content-Type': 'text/html'});
			res.end('<p>Your name is <b>' + qs.parse(body).name + '</b></p>');
		});
	} else {
		res.writeHead(404);
		res.end('Not Found');
	}
}).listen(3000);

client和server通信:

  • 简单例子:
    • server:
      var http = require('http');
      var qs = require('querystring');
      http.createServer(function(req, res) {
      	res.writeHead(200);
      	res.end('Hello World');
      }).listen(3000);
    • client:
      var http = require('http');
      http.request({     //初始化一个http.Client Request对象
      	host: '127.0.0.1',
      	port: 3000,
      	url: '/',
      	method: 'get'
      }, function(res) {
      	var body = '';
      	res.setEncoding('utf8');
      	res.on('data', function(chunk) {
      		body += chunk;
      	});
      	res.on('end', function() {
      		console.log('
       We got: 33[96m' + body + '33[39m
      ');
      	});
      }).end();
  • 通信:

    • server:

      var qs = require('querystring');
      var http = require('http');
      http.createServer(function(req, res) {
      	var body = '';
      	req.on('data', function(chunk) {
      		body += chunk;
      	});
      	req.on('end', function() {
      		res.writeHead(200);
      		res.end('Done');
      		console.log('
       got name 33[90m' + qs.parse(body).name + '33[39m
      ');
      	});
      }).listen(3000);
    • client:

      var http = require('http');
      var qs = require('querystring');
      
      function send(theName) {
      	http.request({
      		host: '127.0.0.1',
      		port: 3000,
      		url: '/',
      		method: 'POST'
      	}, function(res) {
      		res.setEncoding('utf8');
      	    console.log('
       33[90m request complete!33[39m');
      		process.stdout.write('
       your name: ')
      	
         }).end(qs.stringify({name: theName}));
      }
      process.stdout.write('
       your name: ');
      process.stdin.resume();
      process.stdin.setEncoding('utf-8');
      process.stdin.on('data', function(name) {
      	send(name.replace('
      ',''));
      });
      

        

原文地址:https://www.cnblogs.com/jinkspeng/p/4114932.html