nodejs中提供的解析字符串模块;首先需要映入url模块和查询字符串模块
其中url提供的字符串解析方法为parse
const url = require('url');
const querystring = require('querystring');
'use strict'; const http = require('http'); const url = require('url'); const querystring = require('querystring'); const server = http.createServer((req, res) => { let realUrl = 'http://' + req.headers.host + req.url; let urlObj = url.parse(realUrl); //url提供的方法 解析url console.log(urlObj); let qsObj = querystring.parse(urlObj.query); res.writeHead(200, { 'content-type': 'text/html;charset = urf-8' }); res.write('<h1>解析字符串</h1>'); res.write(`用户名是:${qsObj.username}密码是:${qsObj.pwd}`); res.end(); }) server.listen(3003, () => { console.log('server is running at 3003'); })
此时我们就能够获取url上字符串中传的值了