Deno Networking: HTTP Servers and Clients Quiz Quiz

Challenge your understanding of HTTP server and client capabilities in Deno, focusing on how to handle requests, responses, and configure networking tasks. Assess your knowledge of core networking concepts and APIs specific to Deno's modern runtime environment.

  1. Starting a Basic HTTP Server

    Which function is primarily used to start an HTTP server in Deno and listen for incoming requests?

    1. listenHTTP
    2. beginListen
    3. serve
    4. startServer

    Explanation: The 'serve' function is used in Deno to start an HTTP server and listen for requests. 'listenHTTP', 'startServer', and 'beginListen' are not standard Deno APIs for this purpose, although their names may sound plausible or similar. Using the correct function is essential for ensuring your server runs as intended. Remember to import functions with accurate names to avoid runtime errors.

  2. Specifying Server Port

    When setting up an HTTP server in Deno, how do you specify the port number for the server to listen on?

    1. By setting a PORT environment variable only
    2. By embedding the port in the URL path
    3. By passing an options object with a 'port' property
    4. By calling setPort() after server starts

    Explanation: You provide the port number through an options object, commonly with a 'port' property when calling the server function. Embedding it in the URL path does not set the listening port. Just setting a PORT environment variable would not configure the server unless explicitly read. The server cannot be configured after starting using setPort().

  3. Sending HTTP Responses

    Which method is used in Deno to send an HTTP response back to the client within a server handler?

    1. respondWith
    2. writeResponse
    3. serveHTTP
    4. sendResponse

    Explanation: 'respondWith' is the method used to send an HTTP response within a request handler in Deno. 'writeResponse' and 'sendResponse' are incorrect because they are not part of Deno's API. 'serveHTTP' sounds similar but is not the method to send a response; instead, it's involved with adapting handler functions.

  4. Making HTTP Requests

    Which global function is commonly used in Deno to perform an HTTP GET or POST request as a client?

    1. httpRequest
    2. fetch
    3. getRequest
    4. requestHTTP

    Explanation: The global 'fetch' function is the standard way to make HTTP client requests in Deno. 'getRequest', 'httpRequest', and 'requestHTTP' are similar-sounding names but do not exist as global functions in Deno. Choosing the right function is essential for synchronous or asynchronous client-server communication.

  5. Handling JSON Responses

    After performing a fetch request in Deno, which method should you call on the response to parse a JSON body?

    1. json()
    2. readJSON()
    3. bodyToJSON()
    4. parse()

    Explanation: Calling the 'json()' method on the fetch response parses and returns the JSON body as a JavaScript object. 'parse()' does not exist on the response object, and 'readJSON()' and 'bodyToJSON()' are not valid methods in this context, although their names might suggest similar functionality.

  6. Request Method Handling

    How can you check the HTTP request method (such as GET or POST) inside a Deno server request handler?

    1. By calling getMethod(request)
    2. By evaluating request.method
    3. By reading request.type
    4. By parsing request.url

    Explanation: The correct property to check the HTTP method in the request handler is 'request.method'. 'request.type' is not a valid property in this scenario, and 'getMethod(request)' is not a standard function. Parsing the request URL will not provide the HTTP method information.

  7. Serving Static Files

    If you want your Deno server to serve static files, which module or function is commonly used for this?

    1. sendStatic
    2. staticServe
    3. serveDir
    4. serveFileStat

    Explanation: 'serveDir' is used to serve static files and directories in Deno. 'serveFileStat' and 'sendStatic' may sound similar but are not correct Deno standard functions for this purpose. 'staticServe' is also not a recognized function or module in Deno's core environment.

  8. Setting Response Headers

    Which object should you modify to set custom headers such as 'Content-Type' in a Deno HTTP response?

    1. Config object passed to the server
    2. Request.headers property
    3. GlobalHeaders object
    4. Headers object on the Response

    Explanation: To set custom headers like 'Content-Type', modify the Headers object on the Response object being sent. The config object passed to the server is not for setting headers. 'Request.headers' represents headers sent by the client, not ones you send out. 'GlobalHeaders' is not a valid object in this context.

  9. Graceful Shutdown

    What is a recommended practice for stopping a running Deno HTTP server gracefully?

    1. Manually close all client sockets without notification
    2. Remove the 'port' option from the server
    3. Kill the server process immediately
    4. Signal the server to close after finishing active requests

    Explanation: Gracefully stopping the server involves signaling it to finish handling current requests before shutting down. Killing the process immediately can disrupt active connections. Manually closing all client sockets is risky and often leads to errors. Removing the 'port' option from the server does not stop or affect a running instance.

  10. Default Network Interface

    If no hostname or address is specified in the Deno server options, which network interface does the HTTP server listen on by default?

    1. Public Gateway interface
    2. Only the local loopback interface (127.0.0.1)
    3. Primary external network interface
    4. All available network interfaces (0.0.0.0)

    Explanation: Deno's server will by default bind to all available network interfaces, represented as 0.0.0.0, allowing both local and remote connections. Limiting to 127.0.0.1 would restrict access to only local clients. The server does not automatically choose an external or gateway interface unless specifically instructed.