diff --git a/hw3/01-routing.js b/hw3/01-routing.js index d73fdda..e606741 100644 --- a/hw3/01-routing.js +++ b/hw3/01-routing.js @@ -24,6 +24,9 @@ const server = http.createServer((req, res) => { 'other', ]; + // accesses CONST routes list and return an html element + // turns the element content into a hyperlink. + // href specifies the link which the hyperlink goes to let getRoutes = () => { let result = ''; @@ -43,7 +46,67 @@ const server = http.createServer((req, res) => { res.end(); } - // Add your code here + else if (req.url === '/welcome') { + let routeResults = getRoutes(); + + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.write(`

Welcome to HW #3!

`); + res.write(``); + res.end(); + } + + else if (req.url === '/redirect') { + res.writeHead(302, { Location: "/redirected" }); + res.end(); + } + + else if (req.url === '/redirected') { + let routeResults = getRoutes(); + + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.write(`

Redirected to /redirected

`); + res.write(``); + res.end(); + } + + else if (req.url === '/cache') { + let routeResults = getRoutes(); + + res.writeHead(200, { 'Content-Type': 'text/html', 'Cache-control': 'public, max-age=86400'}); + res.write(`

This resource was cached

`); + res.write(``); + res.end(); + } + + else if (req.url === '/cookie') { + let routeResults = getRoutes(); + + res.writeHead(200, { 'Content-Type': 'text/html', 'Set-Cookie':'hello=world'}); + res.write(`

cookies... yummm

`); + res.write(``); + res.end(); + } + + else if (req.url === '/check-cookies') { + let routeResults = getRoutes(); + + res.writeHead(200, { 'Content-Type': 'text/html' }); + + if(req.headers.cookie.indexOf("hello=") !== -1){ + res.write(`

yes

`); + } else { + res.write(`

No

`); + } + res.end(); + } + + else { + res.writeHead(404, {'Content-Type': 'text/html'}); + res.write(`

Error 404 - Page not Found

`); + res.end(); + } + + }); server.listen(port, () => { diff --git a/hw3/02-url.js b/hw3/02-url.js index 00ca7d1..df8aa77 100644 --- a/hw3/02-url.js +++ b/hw3/02-url.js @@ -30,9 +30,75 @@ const server = http.createServer((req, res) => { res.write(`

Exercise 02

`); res.write(``); + } + + + else if (req.url.indexOf('/attributes') >= 0){ + let routeResults = getRoutes(); + const table = createTableHTML(req.url); + + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.write(`

hello world & lorem ipsum

`); + res.write(``); + res.write(table); } - // Add your code here + else if (req.url.indexOf('/items') >= 0){ + let routeResults = getRoutes(); + const table = createTableHTML(req.url); + + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.write(`

hello world & lorem ipsum

`); + res.write(``); + res.write(table); + } + + else if (req.url.indexOf('/characters') >= 0){ + let routeResults = getRoutes(); + const table = createTableHTML(req.url); + + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.write(`

hello world & lorem ipsum

`); + res.write(``); + res.write(table); + } + + function createTableHTML (url) { + let dataset = parsedDataFromURL(url); + let results = ""; + + results += ``; + dataset.forEach( + row => { + results += `` + row.forEach( + data => results += `` + ), + + results += `` + } + ) + results += `
${data}
`; + + return results; + } + + function parsedDataFromURL (url) { + let parsedURL = url.split("?"); + let temp1 = parsedURL[1]; + let parsedArgs = temp1.split("&"); + + let results = []; + let temp = []; + + // [1=1, 2=2...] + parsedArgs.forEach( + arg => results.push(arg.split("=")) + ) + + return results; + } + res.end(); }); diff --git a/hw3/03-form.js b/hw3/03-form.js new file mode 100644 index 0000000..480c449 --- /dev/null +++ b/hw3/03-form.js @@ -0,0 +1,106 @@ +const http = require('http'); + +const port = process.env.PORT || 5000; + +const formHTML = ` + + + + + 03 - Form + + + + +
+

Contact Form

+
Name *
+ +
Email *
+ +
Submit your message:
+ +
+ + +
+ +
+ + +
+ + ` + + + +const server = http.createServer((req, res) => { + const routes = [ + '/form', + '/submit' + ]; + + let body = ""; + + // use the URL interface to work with URLs + // source: https://developer.mozilla.org/en-US/docs/Web/API/URL + let url = new URL(req.url, `http://${req.headers.host}`); + + let getRoutes = () => { + let result = ''; + + routes.forEach( + (elem) => (result += `
  • ${elem}
  • `) + ); + + return result; + }; + + req.on("data", (chunk) => { + body += chunk; + console.log("on data: " + body); + }); + + + req.on("end", () => { + if (req.url === '/') { + let routeResults = getRoutes(); + + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.write(`

    Exercise 03

    `); + res.write(``); + } + + else if (req.url === '/form') { + res.writeHead(200, { 'Content-Type': 'text/html'}); + res.write(formHTML); + } + + else if (req.url === '/submit') { + const params = new URLSearchParams(body); + + res.writeHead(200, { 'Content-Type': 'text/html'}); + res.write(`

    Name: ${params.get("name")}

    `); + res.write(`

    Email: ${params.get("email")}

    `); + res.write(`

    Comments: ${params.get("comment")}

    `); + + if( params.get("newsletter") ){ + res.write(`

    Newsletter: Yes, sign me up for the newsletter.

    `); + } else { + res.write(`

    Newsletter: No, thank you.

    `); + } + } + + res.end(); + }) +}); + +server.listen(port, () => { + console.log(`Server running at http://localhost:${port}`); +}); +