diff --git a/hw4/Claudio_Maggioni/server.js b/hw4/Claudio_Maggioni/server.js index 6878a37..e051606 100755 --- a/hw4/Claudio_Maggioni/server.js +++ b/hw4/Claudio_Maggioni/server.js @@ -1,9 +1,5 @@ #!/usr/bin/env node // vim: set ts=2 sw=2 et tw=80: -/* - * Basic node.js HTTP server - * - */ const http = require('http'); const url = require('url'); @@ -11,10 +7,89 @@ const fs = require('fs'); const routes = Object.create(null); -// Configure your routing table here... -//routes['URL'] = function; +function error(res, code, text = '') { + res.writeHead(code, { 'Content-Type': 'text/html' }); + res.end(` + + + + Error ${code} + + +

Error ${code}

+

${text}

+
+

SA3 - HW4

+ + `); +} -routes['files'] = (req, res) => { +function fileData(reqUrl, prefix, wantExt = true) { + const uri = url.parse(reqUrl).pathname.substring(prefix.length); + const file = __dirname + '/NodeStaticFiles' + uri; + const name = file.substring(file.lastIndexOf('/') + 1); + const ext = wantExt ? name.substring(name.indexOf('.') + 1) : null; + + return { + uri: !uri ? '/' : uri, + file: file, + name: name, + ext: ext + }; +} + +routes['explore'] = (req, res) => { + if (req.method != 'GET') { + error(res, 405, 'Use this URL with only GET requests'); + return; + } + + const { uri, file, name } = fileData(req.url, '/explore', false); + + fs.readdir(file, { withFileTypes: true }, (err, dir) => { + if (err) { + error(res, 404, 'Directory not found'); + return; + } + + const list = [{ name: '.', path: '/explore' + uri }]; + if (uri != '/') { + const parentUri = uri.substring(0, uri.length - name.length - 1); + list.push({ name: '..', path: '/explore' + parentUri }); + } + for (const e of dir) { + list.push({ + name: e.name, + dir: !e.isFile(), + path: ((e.isFile() ? '/file' : '/explore') + uri + + (uri == '/' ? '' : '/') + e.name) + }); + } + + console.log(list); + + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.write(` + + + + ${uri} [DIR] + + +

${uri} [DIR]

+ + + `); + }); +}; + +routes['file'] = (req, res) => { const FILE_TYPES = { html: 'text/html', css: 'text/css', @@ -32,17 +107,16 @@ routes['files'] = (req, res) => { zip: 'application/zip' }; - const uri = url.parse(req.url).pathname.substring('/files'.length); - const file = __dirname + '/NodeStaticFiles' + uri; - const name = file.substring(file.lastIndexOf('/') + 1); - const ext = name.substring(name.indexOf('.') + 1); + if (req.method != 'GET') { + error(res, 405, 'Use this URL with only GET requests'); + return; + } - console.log(file, name, ext); + const { file, name, ext } = fileData(req.url, '/file'); fs.readFile(file, (err, data) => { if (err) { - res.writeHead(404, { 'Content-Type': 'text/html' }); - res.end('File not found: ' + JSON.stringify(err)); + error(res, 404, 'File not found: ' + JSON.stringify(err)); return; } @@ -62,8 +136,7 @@ function onRequest(req, res) { if (typeof routes[uri] === 'function') { routes[uri](req, res); } else { - res.writeHead(404); - res.end(); + error(res, 404, 'Path not found'); } }