url : http://localhost:8080/index.html?t=8851
一,解析url
var pathObj = url.parse(req.url)
解析为:
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?t=8851',
query: 't=8851',
pathname: '/index.html',
path: '/index.html?t=8851',
href: '/index.html?t=8851'
}
二.获取pathname
var Path = pathObj.pathname
得到 : /index.html
三.通过正则获取请求类型
var reg = new RegExp ("([a-z]+)$")
var arr = reg.exec(Path)
得到 : html
四.修改请求头中的Content-Type
res.setHeader("Content-Type","text/"+type)