NodeJS提取网址参数
querystringify模块
querystring,将url请求地址后的参数转化为对象,也可以把对象转化为url后的参数,便利对url的操作,npm install querystring -D,“querystring”已弃用。
const querystring = require(“querystring”);let url = “/get?id=123456&name=%E5%BC%A0%E4%B8%89”;console.log(“uri”, url.split(‘?’)[0]);let param = url.split(‘?’)[1];let obj = querystring.parse(param);console.log(“obj”, obj);console.log(“obj.id=”, obj.id, “obj.name=”, obj.name);let obj2 = { id: 345678, name: “张三”, hobby: [“play”, “com1”, “com2”]};var o2s = querystring.stringify(obj2);console.log(“o2s”, o2s);
querystringify模块
querystringify模块,qs.parse()和qs.stringify(),npm install querystringify –save,地址:https://github.com/unshiftio/querystringify。
代码案例
const qs = require(‘querystringify’);let r11 = qs.parse(‘?foo=bar’);console.log(r11); // { foo: ‘bar’ }let r12 = qs.parse(‘#foo=bar’);console.log(r12); // { foo: ‘bar’ }let r13 = qs.parse(‘foo=bar’);console.log(r13); // { foo: ‘bar’ }let r14 = qs.parse(‘foo=bar&bar=foo’);console.log(r14); // { foo: ‘bar’, bar: ‘foo’ }let r15 = qs.parse(‘foo&bar=foo’);console.log(r15); // { foo: ”, bar: ‘foo’ }let r21 = qs.stringify({ foo: ‘bar’ });console.log(r21); // foo=barlet r22 = qs.stringify({ foo: ‘bar’ }, true);console.log(r22); // ?foo=barlet r23 = qs.stringify({ foo: ‘bar’ }, ‘#’);console.log(r23); // #foo=barlet r24 = qs.stringify({ foo: ” }, ‘&’);console.log(r24); // &foo=
url模块
将url解析成对象,parse方法原型:url.parse(urlStr[, parseQueryString][, slashesDenoteHost]),可传递3个参数,1、urlStr:要解析成对象的url字符串;2、parseQueryString:是否解析查询参数,默认为false;3、slashesDenoteHost:是否以斜线解析主机名,默认为false。
代码案例
const url = require(‘url’)let urlParam = new URL(“/foo?name=%E5%BC%A0%E4%B8%89”, “http://localhost:3000”);console.log(“urlParam =”, urlParam);console.log(“name =”, urlParam.searchParams.get(“name”))
接收请求参数
接收参数,代码案例
const http = require(“http”);const url = require(“url”);// 创建Serverconst httpServer = http.createServer((req, res) => { const method = req.method; const uri = req.url.split(‘?’)[0]; console.log(“uri”, uri); if (uri == ‘/get1’) { // Get请求 let data = url.parse(req.url, true).query; res.setHeader(‘Content-Type’, ‘html/text;charset=utf-8’); console.log(“get1 data”, data); // get1 data [Object: null prototype] { id: ‘123456’, name: ‘张三’ } res.end(“data.id=” + data.id + “,data.name=” + data.name); } else if (uri == ‘/post1’) { // POST请求 let data = url.parse(req.url, true).query; res.setHeader(‘Content-Type’, ‘html/text;charset=utf-8’); console.log(“post1 data”, data); // post1 data [Object: null prototype] { id: ‘123456’, name: ‘李四’ } res.end(“data.id=” + data.id + “,data.name=” + data.name); } else if (uri == ‘/post2’) { // POST请求(JSON) let postData = “”; // 数据块接收中 req.on(“data”, function (postDataChunk) { postData += postDataChunk; }); // 接收完毕 req.on(“end”, function () { let jsonObject = JSON.parse(postData); console.log(“postData=”, postData); res.setHeader(‘Content-Type’, ‘html/text;charset=utf-8’); console.log(“jsonObject.id=”, jsonObject.id); console.log(“jsonObject.name=”, jsonObject.name); res.end(“jsonObject.id=” + jsonObject.id + “,jsonObject.name=” + jsonObject.name); }); }});httpServer.listen(3000, function () { console.log(“http server port:”, 3000);});
模拟实现路由,代码案例
server.js
const http = require(“http”);var url = require(“url”);var router = require(“./module/router.js”);// 创建Serverconst httpServer = http.createServer((request, response) => { if (request.url !== ‘/favicon.ico’) { let pathname = url.parse(request.url).pathname; console.log(“pathname.”, pathname); pathname = pathname.replace(///, ”); console.log(“pathname..”, pathname); router[pathname](request, response); }});// 监听httpServer.listen(3000, function () { console.log(“http server port:”, 3000);});
modulerouter.js
module.exports = { // http://127.0.0.1:3000/demo1 demo1: function (req, res) { res.end(“demo1”); }, // http://127.0.0.1:3000/demo2 demo2: function (req, res) { res.end(“demo2”); }}