hw4: ex3 passes tests

This commit is contained in:
Claudio Maggioni (maggicl) 2019-10-12 14:02:58 +02:00
parent 2dcbefb7db
commit ceccf9ff43
2 changed files with 11 additions and 8 deletions

View file

@ -25,7 +25,8 @@ function error(res, code, text = '') {
} }
function fileData(reqUrl, prefix, wantExt = true) { 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 file = __dirname + '/NodeStaticFiles' + uri;
const name = file.substring(file.lastIndexOf('/') + 1); const name = file.substring(file.lastIndexOf('/') + 1);
const ext = wantExt ? name.substring(name.indexOf('.') + 1) : null; const ext = wantExt ? name.substring(name.indexOf('.') + 1) : null;
@ -52,7 +53,11 @@ routes['explore'] = (req, res) => {
return; return;
} }
const list = [{ name: '.', path: '/explore' + uri }]; const list = [{
name: '.',
path: '/explore' + (uri == '/' ? '' : uri) + '/'
}];
if (uri != '/') { if (uri != '/') {
const parentUri = uri.substring(0, uri.length - name.length - 1); const parentUri = uri.substring(0, uri.length - name.length - 1);
list.push({ name: '..', path: '/explore' + parentUri }); list.push({ name: '..', path: '/explore' + parentUri });
@ -61,13 +66,11 @@ routes['explore'] = (req, res) => {
list.push({ list.push({
name: e.name, name: e.name,
dir: !e.isFile(), dir: !e.isFile(),
path: ((e.isFile() ? '/file' : '/explore') + uri + path: (e.isFile() ? '/file' : '/explore') + uri +
(uri == '/' ? '' : '/') + e.name) (uri == '/' ? '' : '/') + e.name
}); });
} }
console.log(list);
res.writeHead(200, { 'Content-Type': 'text/html' }); res.writeHead(200, { 'Content-Type': 'text/html' });
res.write(`<!DOCTYPE html> res.write(`<!DOCTYPE html>
<html lang="en"> <html lang="en">
@ -95,12 +98,12 @@ routes['file'] = (req, res) => {
css: 'text/css', css: 'text/css',
txt: 'text/plain', txt: 'text/plain',
mp4: 'video/mp4', mp4: 'video/mp4',
ogg: 'video/ogg', ogv: 'video/ogg',
gif: 'image/gif', gif: 'image/gif',
jpg: 'image/jpeg', jpg: 'image/jpeg',
jpeg: 'image/jpeg', jpeg: 'image/jpeg',
png: 'image/png', png: 'image/png',
mpeg: 'audio/mpeg', mp3: 'audio/mpeg',
js: 'application/javascript', js: 'application/javascript',
json: 'application/json', json: 'application/json',
pdf: 'application/pdf', pdf: 'application/pdf',