标签:总结 length lob 常用 res div 简写 大小 require
<form action="http://localhost:8080/" method="get"> 用户:<input type="text" name="user" value="" /><br> 密码:<imput type="password" name="pass" value="" /><br> <input type="submit" value="提交"> </form>
const http = require(‘http‘); http.createServer( (req,res) => { console.log(req.url); // /aaa?user=jason&pass=123456 let GET = {}; let url = req.url; // 判断请求的url中是否含有查询参数 if(url.indexOf(‘?‘) != -1) { let arr = url.split(‘?‘); // arr[0] = 地址 ‘/aaa‘ arr[1] = 数据 ‘user=jason&pass=123456‘ let arr2 = arr[1].split(‘&‘); for(let i = 0; i < arr2.length; i++) { let arr3 = arr2[i].split(‘=‘); // arr3[0] ==>姓名 arr3[1] ==> 密码 GET[arr3[0]] = arr3[1]; } } res.write(‘有请求了‘); res.end(); }).listen(8080);
const http = require(‘http‘); http.createServer( (req, res) => { console.log(req.url); let GET = {}; let url = req.url; if(url.indexOf(‘?‘) != -1) { let arr = url.split(‘?‘); url = arr[0]; let arr2 = arr[1].split(‘&‘); for(let i = 0; i < arr2.length; i++) { let arr3 = arr2[i].split(‘=‘); GET[arr3[0]] = arr3[1]; } } console.log(GET, url) res.write(‘1111111‘); res.end(); }).listen(8799);
const querystring = require(‘querystring‘); let GET = querystring.parse(‘name=jason&age=18‘); console.log(GET); // { name: ‘jason‘, password: ‘123456‘ } ‘/‘ // {} ‘/favicon.ico‘
url模块:
const urlLib = require(‘url‘); let urlObj = urlLib.parse(‘http://www.baidu.com:8901/index/static?name=jason&age=18‘, true); console.log(urlObj);
当第二个参数为true时:
Url { protocol: ‘http:‘, slashes: true, auth: null, host: ‘www.baidu.com:8901‘, port: ‘8901‘, hostname: ‘www.baidu.com‘, hash: null, search: ‘?name=jason&age=18‘, query: { name: ‘jason‘, age: ‘18‘ }, pathname: ‘/index/static‘, path: ‘/index/static?name=jason&age=18‘, href: ‘http://www.baidu.com:8901/index/static?name=jason&age=18‘ }
为false时:
Url { protocol: ‘http:‘, slashes: true, auth: null, host: ‘www.baidu.com:8901‘, port: ‘8901‘, hostname: ‘www.baidu.com‘, hash: null, search: ‘?name=jason&age=18‘, query: ‘name=jason&age=18‘, pathname: ‘/index/static‘, path: ‘/index/static?name=jason&age=18‘, href: ‘http://www.baidu.com:8901/index/static? name=jason&age=18‘ }
const http = require(‘http‘); const urlLib = require(‘url‘); http.createServer( (req, res) => { let urlObj = urlLib.parse(req.url, true); let GET = urlObj.query; let urlName = urlObj.pathname; console.log(GET, urlName); res.write(‘完成‘); res.end(); }).listen(8998);
<form action="http://localhost:8999/aaa" method="post"> 账号:<input type="text" name="name" value=""><br> 密码:<input type="password" name="password" value=""><br> <input type="submit" value="提交"> </form>
const http = require(‘http‘); const querystring = require(‘querystring‘); http.createServer( (req, res) => { // post -- req let str = ‘‘; let i = 0; req.on(‘data‘, (data) => { console.log(`第${i++}次接收到数据`); str += data; }); req.on(‘end‘, () => { let POST = querystring.parse(str); console.log(str); }); }).listen(8999);
const http = require(‘http‘); const fs = require(‘fs‘); const querystring = require(‘querystring‘); const urlLib = require(‘url‘); let server = http.createServer( (req, res) => { // GET let url = urlLib.parse(req.url).pathname; const GET = urlLib.parse(req.url, true).query; // POST let str = ‘‘; let POST; req.on(‘data‘, (data) => { str += data; }); req.on(‘end‘, () => { POST = querystring.parse(str); console.log(url, GET, POST); }); console.log(url, GET, POST); // 文件读取 let file_name = ‘./www‘ + url; fs.readFile(file_name, (err, data) => { if(err) { res.write(‘404‘); }else{ res.write(‘读取成功‘); } res.end(); }) }); server.listen(8999);
nodejs学习笔记二(get请求、post请求、 querystring模块,url模块)
标签:总结 length lob 常用 res div 简写 大小 require
原文地址:http://www.cnblogs.com/jasonwang2y60/p/7062053.html