Node.js GET/POST请求
获取GET请求内容
由于GET请求直接被嵌入在路径中,URL是完整的请求路径,包括了?后面的部分,因此你可以手动解析后面的内容作为GET请求的参数。
node.js 中 url 模块中的 parse 函数提供了这个功能。
实例
const http = require('http');
const url = require('url');
const 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(8090)
在浏览器中访问 http://localhost:8090/user?name=ahri&desc=我永远喜欢阿狸 ,可以看到:
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search:
'?name=ahri&desc=%E6%88%91%E6%B0%B8%E8%BF%9C%E5%96%9C%E6%AC%A2%E9%98%BF%E7%8B%B8',
query: [Object: null prototype] { name: 'ahri', desc: '我永远喜欢阿狸' },
pathname: '/user',
path:
'/user?name=ahri&desc=%E6%88%91%E6%B0%B8%E8%BF%9C%E5%96%9C%E6%AC%A2%E9%98%BF%E7%8B%B8',
href:
'/user?name=ahri&desc=%E6%88%91%E6%B0%B8%E8%BF%9C%E5%96%9C%E6%AC%A2%E9%98%BF%E7%8B%B8' }
获取 URL 的参数
我们可以使用 url.parse 方法来解析 URL 中的参数,代码如下:
const http = require('http');
const url = require('url');
const util = require('util');
http.createServer(function(req,res) {
res.writeHead(200,{'Content-type': 'text/plain; charset=utf-8'})
// 解析url参数
let params = url.parse(req.url,true).query;
console.log(params);
res.write('网站名:'+params.name);
res.write('\n');
res.write('网站 desc:'+params.desc);
res.end();
}).listen(8090)
在浏览器访问 http://localhost:8090/user?name=ahri&desc=我永远喜欢阿狸 ,可以看到:
网站名:ahri
网站 desc:我永远喜欢阿狸
获取POST请求内容
POST 请求的内容全部的都在请求体中,http.ServerRequest 并没有一个属性内容为请求体,原因是等待请求体传输可能是一件耗时的工作。
比如上传文件,而很多时候我们可能并不需要理会请求体的内容,恶意的POST请求会大大消耗服务器的资源,所以 node.js 默认是不会解析请求体的,当你需要的时候,需要手动来做。
实例:
基本语法结构说明:
const http = require('http');
const querystring = require('querystring');
const util = require('util');
http.createServer(function(req,res) {
// 定义了一个post变量,用于暂存请求体的信息
let post = '';
// 通过req的data事件监听函数,每当接受到请求体的数据,就累加到post中
req.on('data',function(chunk) {
post += chunk;
})
// 在end事件触发后,通过querystring.parse将POST解析为真正的post请求格式,然后向客户端返回
res.on('end',function() {
post = querystring.parse(post);
res.end(util.inspect(post));
})
}).listen(8090)
实例,通过表单提交发送post数据:
const http = require('http');
const querystring = require('querystring');
let postHtml =
'<html><head><meta charset="utf-8"><title>菜鸟教程 Node.js 实例</title></head>' +
'<body>' +
'<form method="post">' +
'网站名: <input name="name"><br>' +
'网站 URL: <input name="url"><br>' +
'<input type="submit">' +
'</form>' +
'</body></html>';
http.createServer(function(req,res) {
let body = '';
req.on('data',function(chunk) {
body += chunk;
})
req.on('end',function() {
// 解析参数
body = querystring.parse(body);
// 设置响应头的信息及编码
res.writeHead(200,{'Content-type': 'text/html; charset=utf8'});
if(body.name && body.url) {
res.write('网站名:'+body.name);
res.write('<br>');
res.write('网站 url:'+body.url);
}else {
res.write(postHtml);
}
res.end();
})
}).listen(8090)
结果如下:
![[外链图片转存失败(img-tEafMCcP-1563882683256)(readme_files/1.gif)]](https://i-blog.csdnimg.cn/blog_migrate/693a1d5a5195dc855a68329085660e66.png)
Node.js Web模块
使用node创建web服务器
实例:
const http = require('http');
const fs = require('fs');
const url = require('url');
// 创建服务器
http.createServer(function(req,res) {
// 解析请求,包括文件名
let pathname = url.parse(req.url).pathname;
// 输出请求的文件名
console.log('Request for '+pathname+'recived!');
// 从文件系统中读取请求文件的内容
fs.readFile(pathname.substr(1),function(err,data) {
if(err) {
console.log(err);
// HTTP状态码404 NOT FOUND
// Content-type text/html
res.writeHead(404,{'Content-type': 'text/html'});
}else {
// HTTP状态码200: OK
// Content-type text/html
res.writeHead(200,{'Content-type': 'text/html'});
// 相应文件内容
res.write(data.toString());
}
// 发送相应数据
res.end();
})
}).listen(8090)
创建index.html,内容如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>我永远喜欢阿狸</h1>
<h2>我永远喜欢学习</h2>
</body>
</html>
访问 http://localhost:8090/index.html ,可以看到
![[外链图片转存失败(img-YjIbwHBt-1563882683259)(readme_files/1.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/547b85760d9ae56a8f72a92a1592bb49.png)
使用node创建web客户端
实例:
在上一个服务开启的情况下,重新创建文件client.js,内容如下:
const http = require('http');
// 用于请求的选项
let options = {
host: 'localhost',
port: '8090',
path: '/index.html'
}
// 处理响应的回调函数
let callback = function(res) {
// 不断更新数据
let body = '';
res.on('data',function(chunk) {
body += chunk;
})
res.on('end',function() {
// 数据接收完成
console.log(body);
})
}
// 向服务端发送数据
let req = http.request(options,callback);
req.end();
执行 node client.js ,可以看到输出如下:
>node demo.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>我永远喜欢阿狸</h1>
<h2>我永远喜欢学习</h2>
</body>
</html>
服务器端可以看到输出信息:
Request for /index.htmlrecived!

456

被折叠的 条评论
为什么被折叠?



