2.POCE.4: File Server


Start with the base Node repo.


Create a file server using Node.js with the following attributes.
  1. 1.
    It listens for GET requests and sends back files.
  2. 2.
    It listens on the port specified in the command to start the server. The following command sets the port to 3004.
    node index.js 3004
  3. 3.
    It responds with status code 200 when it finds and sends back a file.
  4. 4.
    It responds with "Sorry we couldn't find your page!" and status code 404 when it doesn't find a file.
  5. 5.
    Put your Project 1: Video Poker files in the file server folder. Try to access your video poker project at localhost:3004


We use content type headers in HTTP responses to communicate to the client the type of data in the response content. We will update our file server to incorporate this. Reference this example for how to use content type response headers, and read more about content type headers here.
The content type example above uses require statements, which will not work in the environment in which we are running Node. Use import statements instead.
Update our file server with the following features.
  1. 1.
    It only sends a file back when it can detect the file type of the requested file (e.g. by file extension).
  2. 2.
    It sets the correct content type in the header of the response.
  3. 3.
    It sends back an HTML 404 page when it can't find the requested file.
  4. 4.
    Create an "easter egg" so that whenever a user visits /banana they get the response "hello banana!".
  5. 5.
    On every response create an easter egg header of 'rocket-academy-secret-word' in the response. Set the value to 'blastoff'.

Reference solution

Click here to see the reference solution for this post class.