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.
Which function is primarily used to start an HTTP server in Deno and listen for incoming requests?
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.
When setting up an HTTP server in Deno, how do you specify the port number for the server to listen on?
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().
Which method is used in Deno to send an HTTP response back to the client within a server handler?
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.
Which global function is commonly used in Deno to perform an HTTP GET or POST request as a client?
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.
After performing a fetch request in Deno, which method should you call on the response to parse a JSON body?
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.
How can you check the HTTP request method (such as GET or POST) inside a Deno server request handler?
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.
If you want your Deno server to serve static files, which module or function is commonly used for this?
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.
Which object should you modify to set custom headers such as 'Content-Type' in a Deno HTTP 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.
What is a recommended practice for stopping a running Deno HTTP server gracefully?
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.
If no hostname or address is specified in the Deno server options, which network interface does the HTTP server listen on by default?
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.