WebFund 2024F: Assignment 1: Difference between revisions
Line 22: | Line 22: | ||
<li>[1] What mistakes, errors, or other problems arose while you were developing your solution?</li> | <li>[1] What mistakes, errors, or other problems arose while you were developing your solution?</li> | ||
</ol> | </ol> | ||
<li>[4] How could you change line 80 (the assignment of <tt>origpath</tt>) so that it directly extracted the path (pathname) from the request URL string, instead of first converting it into a URL object? You can use any built-in JavaScript string manipluation facilities, including regular expressions. Make sure your solution does not include the protocol, hostname, port number, query, or fragment. | <li>[4] How could you change line 80 of simpleserver2.js (the assignment of <tt>origpath</tt>) so that it directly extracted the path (pathname) from the request URL string, instead of first converting it into a URL object? You can use any built-in JavaScript string manipluation facilities, including regular expressions. Make sure your solution does not include the protocol, hostname, port number, query, or fragment. | ||
<ol style="list-style-type:lower-alpha"> | <ol style="list-style-type:lower-alpha"> | ||
<li>[2] What is your solution? Please give the precise code.</li> | <li>[2] What is your solution? Please give the precise code.</li> |
Revision as of 02:41, 27 September 2024
This assignment is not yet finalized.
Please submit the answers to the following questions via Brightspace by October 4, 2024. There are ?? points in ?? questions.
Submit your answers as a plain text file following this template. Name your answer file "comp2406-assign1-<username>.txt" (where username is your MyCarletonOne username). Please make sure to use your correct student ID number otherwise your grades may not be properly recorded.
Your answers will be parsed by a script in order to help with grading so please preserve the format of the template. No other formats will be accepted.
Note that the file should be a text file, preferably a UNIX text file (so LF line endings, not CRLF). (See the Wikipedia page on text files to learn more.)
You may use this validator page to make sure your answer file is properly named and formatted.
Don't forget to include what outside resources you used to complete each of your answers, including other students, man pages, and web resources (including AI services). You do not need to list help from the instructor, TA, or information covered in lecture or tutorial.
Questions
- [4] How could you change simpleserver2 so that it loaded the page not found template from a file (rather than it being included in the source)? The template should be loaded when the server starts.
- [2] What code needs to be added, changed, or removed? Be precise.
- [1] Why do you think your solution is correct?
- [1] What mistakes, errors, or other problems arose while you were developing your solution?
- [4] How could you change line 80 of simpleserver2.js (the assignment of origpath) so that it directly extracted the path (pathname) from the request URL string, instead of first converting it into a URL object? You can use any built-in JavaScript string manipluation facilities, including regular expressions. Make sure your solution does not include the protocol, hostname, port number, query, or fragment.
- [2] What is your solution? Please give the precise code.
- [1] How did you test your solution to make sure it was correct?
- [1] What mistakes, errors, or other problems arose while you were developing your solution?
Code
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2024 Anil Somayaji
//
// simpleserver2.js
// for COMP 2406 (Fall 2024), Carleton University
//
// Initial version: Sept 26, 2024
//
// run with the following command:
// deno run --allow-net --allow-read simpleserver2.js
//
const status_NOT_FOUND = 404;
const status_OK = 200;
function MIMEtype(filename) {
const MIME_TYPES = {
'css': 'text/css',
'gif': 'image/gif',
'htm': 'text/html',
'html': 'text/html',
'ico': 'image/x-icon',
'jpeg': 'image/jpeg',
'jpg': 'image/jpeg',
'js': 'text/javascript',
'json': 'application/json',
'png': 'image/png',
'txt': 'text/text'
};
var extension = "";
if (filename) {
extension = filename.slice(filename.lastIndexOf('.')+1).toLowerCase();
}
return MIME_TYPES[extension] || "application/octet-stream";
};
function template_header(title) {
return `<!DOCTYPE html>
<html>
<head>
<title>${title}</title>
</head>
`
}
function template_notFound(path) {
return template_header("Page not found") +
`<body>
<h1>Page not found</h1>
<p>Sorry, the requested page was not found.</p>
</body>
</html>
`
}
async function fileData(path) {
var contents, status, contentType;
try {
contents = await Deno.readFile("./www" + path);
status = status_OK;
contentType = MIMEtype(path);
} catch (e) {
contents = template_notFound(path);
status = status_NOT_FOUND;
contentType = "text/html";
}
return { contents, status, contentType };
}
async function handler(req) {
var origpath = new URL(req.url).pathname;
var path = origpath;
if (path === "/") {
path = "/index.html";
}
var r = await fileData(path);
console.log(`${r.status} ${req.method} ${r.contentType} ${origpath}`);
return new Response(r.contents,
{status: r.status,
headers: {
"content-type": r.contentType,
}});
}
Deno.serve(handler);