1.GET / POST请求
1.1获取GET请求
由于GET请求直接被嵌入在路径中,URL是完整的请求路径,包括了?后面的部分,因此你可以手动解析后面的内容作为GET请求的参数。
node.js 中url模块中的parse函数提供了这个功能。
util是node.js常用的工具模块。util.inspect() 是一个将任意对象转换为字符串的方法,通常用于调试和错误输出
//GET请求练习
var http = require("http");
var url = require("url");
var util = require("util");
//创建服务器
http.createServer(function(req,res){
//输出响应式头
res.writeHead(200,{"Content-Type":'text/plain;charset=utf-8'});
res.end(util.inspect(url.parse(req.url,true)))
}).listen(8888)
console.log(`服务器已启动。。访问:http://127.0.0.1:8888`);
2.2获取POST请求内容
POST请求的内容全部的都在请求体中,http.ServerRequest并没有一个属性内容为请求体,原因是等待请求体传输可能是一件耗时的工作。
const http = require('http')
const querystring = require('querystring')
//表单页面内容
var postHtml =
'<html><head><meta charset="utf-8"><title>编程 Node.js实例</title></head>'+
'<body>'+
'<form method="post">'+
'姓名:<input name="name"><br>'+
'班级:<input name="class"><br>'+
'<input type="submit">'+
'</form>'+
'</body></html>';
http.createServer(function(req,res){
//定义一个body变量,用于暂存请求信息
var body='';
//通过req的data事件监听函数 每当接收到请求的数据,加到body变量中
req.on("data",function(turck){
// +=默认将内容转换成字符串
body+=turck;
})
//在end事件触发后,通过querystring.parse将post解析为真正的POST请求格式,然后向客户端返回
req.on ("end",function(){
//解析参数
body = querystring.parse(body);
//设置响应头部信息及编码
res.writeHead(200,{"Content-Type":'text/html;charset=utf-8'});
if(body.name && body.class){
//执行提交操作,获取提交信息,并输出
res.write(`姓名:${body.name}`)
res.write(`<br>`)
res.write(`班级:${body.class}`)
}else{
//说明是第一次加载,需要显示表单
res.write(postHtml)
}
res.end()
})
}).listen(8888)
// console.log(`服务器已启动...访问:http://127.0.0.1:8888`);
2.Web模块
2.1 什么是Web服务器
Web服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序。
Web服务器的基本功能就是提供Web信息浏览服务。它只需支持HTTP协议、HTML文档格式及URL,与客户端的网络浏览器配合。
大多数Web服务器都支持服务端的脚本语言(Java、C#、php、python)等,并通过脚本语言从数据库获取数据,将结果返回给客户端浏览器。
目前最主流的三个Web服务器是Apache.Nginx、IlS。
2.2 Web应用架构
Client-客户端,一般指浏览器,浏览器可以通过HTTP协议向服务器请求数据。
Server-服务端,一般指Web服务器,可以接收客户端请求,并向客户端发送响应数据。
Business -业务层,通过Web服务器处理应用程序,如与数据库交互,逻辑运算,调用外部程序等。 Data -**数据层**,一般由数国库组成。
2.3 使用Node创建Web服务器
const http = require('http')
const url = require('url')
const fs = require('fs')
http.createServer(function(request,response){
let pathname = url.parse(request.url).pathname;
// console.log(pathname);
if(!pathname || pathname=='/'){
pathname='/index.html'
}
//读取文件 index.html
fs.readFile(pathname.substr(1),function(err,data){
if(err){
response.writeHead(404,{"Content-Type":"text/html"})
}else{
response.writeHead(200,{"Content-Type":"text/html"})
response.write(data.toString())
}
response.end()
})
}).listen(8888)
index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
欢迎
</body>
</html>
2.4 使用Node创建Web客户端
const http=require('http')
let options={
host:'localhost',
port:8888,
path:'/index.html'
}
function callback(res){
var body=''
res.on('data',function(truck){
body+=truck
})
res.on('end',function(){
console.log(body);
})
}
var req=http.request(options,callback)
req.end()
运行标题2.4时 先运行标题2.3 后运行标题2.4