版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012326462/article/details/83107512
url模块可以用来解析url的请求
var url = require('url')
var http = require('http')
var server = http.createServer()
server.on('request', function(req, res) {
console.log(req.url)
// /abc/d/ef?name=aa&age=b
var parseObj = url.parse(req.url, true)
//{"protocol":null,"slashes":null,"auth":null,"host":null,"port":null,"hostname":null,"hash":null,"search":"?name=aa&age=b","query":{"name":"aa","age":"b"},"pathname":"/abc/d/ef","path":"/abc/d/ef?name=aa&age=b","href":"/abc/d/ef?name=aa&age=b"}
console.log(JSON.stringify(parseObj))
res.end(JSON.stringify(parseObj))
})
server.listen(4000, function() {
console.log('server is running')
})
url.parse第一个参数是网址,第二个参数为true会把请求参数 query里面的内容格式化成对象,否则是字符串,也就是search中?后面的内容。