Extension Exercises

If you've finished with the other exercises, here are some extensions you can attempt. You can modify your TODO list, or one of the example apps.

  1. Simplify how handlers are written, so that you can write a function that just receives a request object, and returns an object with status, body, headers

    Before:
    
              http.createServer(
                function(request, response) {
                  const page = "...";
                  response.writeHead(200, {"Content-Type": "text/html"});
                  response.end(page)
                }
              )
            
    After:
    
              http.createServer(
                convertSimpleServer(
                  function(request) {
                     return {status: 200,
                             headers: {"Content-Type": "text/html"},
                             body: "..."};
                  }
                )
              )
            
  2. Make a "data-driven" router, ie. instead of several if statements, make it so that you can use an object to define routing.

    Before:
    
               http.createServer(
                 function(request, response) {
                   if(request.url === "/add" && request.method === "POST") {
                     ...
                   } else if (request.url === "/" && request.method === "GET") {
                     ...
                   }
                 }
               )
            
    After:
    
               http.createServer(
                 convertDataDrivenServer(
                   {"/" {GET: (request) => {
                                 ...
                              }},
                    "/add" {POST: (request) => {
                                    ...
                                  }}}
                 )
               )
            
  3. Make use of the EJS template library to generate HTML from templates in seperate files
  4. Rewrite your app using the Express library