diff --git a/hw4/Claudio_Maggioni/NodeStaticFiles/icons/.gitkeep b/hw4/Claudio_Maggioni/NodeStaticFiles/icons/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/hw4/Claudio_Maggioni/server.js b/hw4/Claudio_Maggioni/server.js index e051606..dabb55d 100755 --- a/hw4/Claudio_Maggioni/server.js +++ b/hw4/Claudio_Maggioni/server.js @@ -25,7 +25,8 @@ function error(res, code, text = '') { } function fileData(reqUrl, prefix, wantExt = true) { - const uri = url.parse(reqUrl).pathname.substring(prefix.length); + const uri = decodeURIComponent(url.parse(reqUrl).pathname + .substring(prefix.length)).replace(/\/+$/, ''); const file = __dirname + '/NodeStaticFiles' + uri; const name = file.substring(file.lastIndexOf('/') + 1); const ext = wantExt ? name.substring(name.indexOf('.') + 1) : null; @@ -52,7 +53,11 @@ routes['explore'] = (req, res) => { return; } - const list = [{ name: '.', path: '/explore' + uri }]; + const list = [{ + name: '.', + path: '/explore' + (uri == '/' ? '' : uri) + '/' + }]; + if (uri != '/') { const parentUri = uri.substring(0, uri.length - name.length - 1); list.push({ name: '..', path: '/explore' + parentUri }); @@ -61,13 +66,11 @@ routes['explore'] = (req, res) => { list.push({ name: e.name, dir: !e.isFile(), - path: ((e.isFile() ? '/file' : '/explore') + uri + - (uri == '/' ? '' : '/') + e.name) + path: (e.isFile() ? '/file' : '/explore') + uri + + (uri == '/' ? '' : '/') + e.name }); } - console.log(list); - res.writeHead(200, { 'Content-Type': 'text/html' }); res.write(` @@ -95,12 +98,12 @@ routes['file'] = (req, res) => { css: 'text/css', txt: 'text/plain', mp4: 'video/mp4', - ogg: 'video/ogg', + ogv: 'video/ogg', gif: 'image/gif', jpg: 'image/jpeg', jpeg: 'image/jpeg', png: 'image/png', - mpeg: 'audio/mpeg', + mp3: 'audio/mpeg', js: 'application/javascript', json: 'application/json', pdf: 'application/pdf',