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) {
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(`<!DOCTYPE html>
<html lang="en">
@ -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',