所以我有这个:
const server = http.createServer(function(req,res){
const url = req.url;
const q = qs.parse(url);
console.log('querystring =>',q);
if(q.path){
fs.createReadStream(q.path).pipe(res);
}
else {
res.statusCode = 500;
res.end(JSON.stringify({error: 'no path or bundle.'}))
}
});
我为'querystring'
querystring => { '/suman_testing?data': '{"path":"/Users/alexamil/WebstormProjects/oresoftware/sumanjs/suman/test/src/dev/browser/test-file.html"}' }
querystring => { '/favicon.ico': '' }
呃,我该怎么办?我原以为默认是解析?
字符后的所有内容。
这是通过?
字符拆分字符串并在之后解析所有内容的正确方法吗?
答案 0 :(得分:0)
这比我预期的要简单一点,这就是我的工作方式:
const url = require('url');
let httpServer = http.createServer(function (req, res) {
const {query} = url.parse(req.url, true);
// query is now a plain JS key/value object, parsed from query string
});