Thursday 26 October 2017

[Advice] How to give files back?

I'm trying to give a file that someone types as path in the url. I have some thing like:else if (regex.test(theURL)) { //checks if filename is valid var test = giveFILE(filename); if (test==1){ //filetype=mp3 var song = fs.readFile('/' +filename); response.writeHead(200, {'Content-Type': 'audio/mp3'}); response.end(song, 'binary'); } else if (test==2){ //filetype=jpg console.log('after regex filename: '+ filename); var img = fs.readFile(filename); response.writeHead(200, {'Content-Type': 'image/jpg'}); response.end(img, 'binary'); } else if (test==3){ fs.readFile(filename, function(err, data) { // error handling if the url is bad if (err) { response.writeHead(403, {'Content-Type': 'text/html'}); return response.end("403 Not Found");}}); } } I struggling with this. Why won't it give me the file I want? I have the right filename format.

Submitted October 26, 2017 at 08:07PM by ImpatientTomato

No comments:

Post a Comment