Coder Social home page Coder Social logo

node-http-static-lab's Introduction

Node Static File Server

Objectives

  1. Implement rudimentary static web server
  2. Use the fs (file system) module which will serve some HTML file like Apache or Nginx web server
  3. Create index.html that will be served by the Node server

Introduction

Every web page on the Internet is served by some HTTP web server. For this simple reasons, building web servers is an important skill to have. Single-page applications and static web pages (static files) which don't require server-side logic, can be served by static HTTP web servers like Apache, or Nginx.

In this lab, you'll build a static file server to serve static files over the HTTP protocol.

Instructions

  1. Create file server.js
  2. Implement an HTTP server with http
  3. Read request.url to get the file name.
  4. Boot the server with the value from a command-line argument or if not set value 3000.
  5. Read the file and serve it to the client. Handle the errors accordingly when reading files (404 Not Found).
  6. Server must send back 200 on success (along with the file content) and 404 when file not found along with 404 Not Found\n body.
  7. Run tests with npm install && npm test, or just npm test.

Extra Info

Here are some hints:

  1. To read request.url to get the file name, use path.join().
  2. To boot the server with the value from command-line argument or if not set value 3000, use port = process.argv[2] || 3000.
  3. To read the file and serve it to the client, use binary mode as in fs.readFile(filename, 'binary', function(error, file) {...}

Bonus

You can score bonus points for playing with the your code using debugger. There are multiple way to do so. You can use next (n) or step (s), but in most cases you would have to look at many statements if you want to get to the callback (i.e., request handler). The easiest way to do it, is to put a debugger statement in the callback, start the server in debug mode and submit a request with CURL or browser.

To put a debugger statement, simply type debugger in the server.js file and save it. To start the server in debug mode, use node debug server, then press c for continue (n is better, because you can move to the next statement). The server will listen for incoming requests. Now, submit your request with CURL, Postman or just by going to the browser. This will stop the debugger at your breakpoint. You can use repl or any other commands now. Enjoy the show!

node-http-static-lab's People

Contributors

azat-co avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.