Node.js 处理get和post请求参数
2021-05-30 13:00
标签:转化 post请求 sla sea search creates 格式 get com Node.js 处理get和post请求参数 标签:转化 post请求 sla sea search creates 格式 get com 原文地址:https://www.cnblogs.com/webxjz/p/14672461.html/*
get参数处理-url核心模块
*/
const url = require(‘url‘);
// --------------parse方法的作用就是把URL字符串转化为对象-----------
let str = ‘http://www.baidu.com/abc/qqq?flag=123&keyword=java‘;
let ret = url.parse(str,true);
console.log(ret.query.keyword); // 获取变量名为keyword的值
// ------format的作用就是把对象转化为标准的URL字符串------
let obj = {
protocol: ‘http:‘,
slashes: true,
auth: null,
host: ‘www.baidu.com‘,
port: null,
hostname: ‘www.baidu.com‘,
hash: null,
search: ‘?flag=123&keyword=java‘,
query: ‘flag=123&keyword=java‘,
pathname: ‘/abc/qqq‘,
path: ‘/abc/qqq?flag=123&keyword=java‘,
href: ‘http://www.baidu.com/abc/qqq?flag=123&keyword=java‘
};
let ret1 = url.format(obj);
console.log(ret1);
/*
get参数解析
*/
const http = require(‘http‘);
const path = require(‘path‘);
const url = require(‘url‘);
http.createServer((req,res)=>{
let obj = url.parse(req.url,true);
res.end(obj.query.username + ‘=========‘ + obj.query.password);
}).listen(3000,()=>{
console.log(‘running....‘);
})
/*
post参数处理
*/
const querystring = require(‘querystring‘);
const http = require(‘http‘);
// parse方法的作用就是把字符串转成对象
let param = ‘username=lisi&password=123‘;
let obj = querystring.parse(param);
console.log(obj);
// stringify的作用就是把对象转成字符串
let obj1 = {
flag : ‘123‘,
abc : [‘hello‘,‘hi‘]
}
let str1 = querystring.stringify(obj1);
console.log(str1);
http.createServer((req,res)=>{
if(req.url.startsWith(‘/login‘)){
let pdata = ‘‘;
req.on(‘data‘,(chunk)=>{
// 每次获取一部分数据
pdata += chunk;
});
req.on(‘end‘,()=>{
// 这里才能得到完整的数据
console.log(pdata);
let obj = querystring.parse(pdata);
res.end(obj.username+‘-----‘+obj.password);
});
}
}).listen(3000,()=>{
console.log(‘running...‘);
})
/*
登录验证功能
*/
const http = require(‘http‘);
const url = require(‘url‘);
const querystring = require(‘querystring‘);
const ss = require(‘./06.js‘);
http.createServer((req,res)=>{
// 启动静态资源服务
if(req.url.startsWith(‘/www‘)){
ss.staticServer(req,res,__dirname);
}
console.log(req.url);
// 动态资源
if(req.url.startsWith(‘/login‘)){
// get请求
if(req.method == ‘GET‘){
let param = url.parse(req.url,true).query;
if(param.username == ‘admin‘ && param.password == ‘123‘){
res.end(‘get success‘);
}else{
res.end(‘get failure‘);
}
}
// post请求
if(req.method == ‘POST‘){
let pdata = ‘‘;
req.on(‘data‘,(chunk)=>{
pdata += chunk;
});
req.on(‘end‘,()=>{
let obj = querystring.parse(pdata);
if(obj.username == ‘admin‘ && obj.password == ‘123‘){
res.end(‘post success‘);
}else{
res.end(‘post failure‘);
}
});
}
}
}).listen(3000,()=>{
console.log(‘running....‘);
});
const path = require(‘path‘);
const fs = require(‘fs‘);
const mime = require(‘./mime.json‘);
exports.staticServer = (req,res,root) => {
fs.readFile(path.join(root,req.url),(err,fileContent)=>{
if(err){
// 没有找到对应文件
res.writeHead(404,{
‘Content-Type‘:‘text/plain; charset=utf8‘
});
res.end(‘页面被狗狗叼走了‘);
}else{
let dtype = ‘text/html‘;
// 获取请求文件的后缀
let ext = path.extname(req.url);
// 如果请求的文件后缀合理,就获取到标准的响应格式
if(mime[ext]){
dtype = mime[ext];
}
// 如果响应的内容是文本,就设置成utf8编码
if(dtype.startsWith(‘text‘)){
dtype += ‘; charset=utf8‘
}
// 设置响应头信息
res.writeHead(200,{
‘Content-Type‘:dtype
});
res.end(fileContent);
}
});
}
下一篇:autofac for mvc