Directly on the code, this is the first version, you can judge the extension
varHTTP = require (' http ');varExpress = require (' Express ');varFs=require ("FS");varPath=require ("Path");varMIME = require (' MIME '));varApp =Express ();varCurrdir = ' F:\\users\\djyk\\74dj.mp3 '; App.get (‘*‘,function(req, res, next) {varReqpath =decodeURI (Req.path); Console.log (Reqpath); varfilepath =Path.join (Currdir,reqpath); Fs.lstat (filepath,function(err, stat) {if(err) {if(Err.code = = = ' ENOENT ')) {Res.writehead (404); Res.end ("404 Not Found"); return; } res.writehead (500); Res.end (Json.stringify (err)); return; } if(Stat.isdirectory ()) {Res.writehead (403); Res.end ("403 Forbidden"); return; } if(Path.extname (filepath)!== '. mp3 ') {Res.writehead (400); Res.end ("Bad Request"); return; } varf =Fs.createreadstream (filepath); Const ROW= {}; row[' Content-type '] =Mime.gettype (Reqpath); row[' content-disposition '] = ' attachment; Filename= ' +encodeURI (Path.basename (Reqpath)); Res.writehead (200, Row); F.pipe (RES); });}); Http.createserver (APP). Listen (50074);
node. JS builds an HTTP server that provides file downloads