你好,我必须做一个功课,我不知道为什么不工作。我必须处理一个POST请求,发送一个gz文件,我必须在路径参数指定的路径中解压缩...我不知道为什么它不起作用...也许我是愚蠢的使用curl命令:P
function handle_post(req,res,path,cb){
function warn(err,event){
if(err)
console.warn(`Request ${event}, could not close ${path}`)
else
console.warn(`Request ${event}, ${path} closed succesfully`)
}
if( Fs.existsSync(path) )
return cb(400,JSON.stringify({error: "file already exist"}));
var out= Fs.createWriteStream(path);
res.pipe( Zlib.createGunzip() ).pipe( out );
out.on('finish',() => cb(200,JSON.stringify({error : null, "written bytes":out.bytesWritten})+'\n'));
req.on('aborted',() => warn(err,'aborted') );
req.on('error',(err)=>{
cb(500,JSON.stringify({error : err.message, "written bytes":ws.bytesWritten})+'\n')
});
}
var s=Http.createServer(
(req,res) => {
console.log("Request: "+req.method+" URL: "+req.url);
function send(code,json_string){
res.writeHead(code,{"Content-Type" : "application/json"});
res.end(json_string)
}
if(req.method=='GET')
handle_get(home+req.url,send);
else if(req.method=='POST'){
var parsed_url=Url.parse(req.url,true);
var path=parsed_url.query.path;
if(!path)
return send(400,JSON.stringify({error : 'Missing path'})+'\n');
handle_post(req,res,home+'/'+path,send);
}
});
s.listen(8080);
答案 0 :(得分:0)
我做了一个迷雾,解决方案是:req.pipe( Zlib.createGunzip() ).pipe( out );
这是必须通过管道输送的请求