Giter Site home page Giter Site logo

cen3031-fall-lab1's Introduction

Introduction to HTTP and Asynchronous Programming using Node.js

In this assignment we will start to build our Football Club application. We will use Node.js and some of its built in modules to implement a server that provides directory data to clients.

What is HTTP? (Make sure to read this)

HTTP (Hypertext Transfer Protocol) is a stateless protocol that allows computers to communicate with each other. We use HTTP to allow our client application (the one users see) to communicate with a server that stores and manipulates data relevant to the user.

HTTP basically boils down to a request and a response. A client makes a request to either retrieve, add, delete, or modify data in some fashion. The host receives this request, and will provide an appropriate response back to the client. In our case, the server will handle requests for the directory footballClub by responding with the schools data in the JSON format.

What is Node.js?

Node.js is a Javascript runtime environment built on Google's V8 engine. In other terms, it is a program that interprets Javascript. If I made a file named hello.js with the line

console.log('Hello, world!');

and then typed the command node hello.js in my terminal, I should expect to see the text Hello, world! printed on the screen.

Node is well known for its ability to run code asynchronously. This means that input and output are non-blocking, and the process of one function does not stop the execution of another. The way this asynchronous code is implemented is through a callback functions, which are called after a certain process has been completed. The best way to illustrate this is through example.

This is a simple server that responds to all requests with the text Request received!.

import * as http from 'http';
 
const port = 5000; 

const requestHandler = (request, response) => {
  response.end('Request received!');
};

// a server is created, but not started
const server = http.createServer(requestHandler);
//the above function can also be written as
  /**
   * const server = http.createServer((req, res) => {
   * requestHandler(req, res)
   * });
   */

// the server is now started, listening for requests on port 5000
server.listen(port, () => {
  //once the server is listening, this callback function is executed
  console.log(`Server listening on: http://127.0.0.1: ${port}`);
});
console.log('Is the server started?');

Which log statement do you expect to be printed first? Answer this, then type the command node simpleServer.js and see if the results match up with what you were thinking.

Is the server started? gets printed first is because the call to server.listen() is asynchronous in nature. While server.listen() is not finished, the control flow gets passed to the next line of the program. Once server.listen() is finished, it executes the callback, defined by the anonymous function:

() => {
    console.log(`Server listening on: http://127.0.0.1: ${port}`);
}

Before continuing to the assignment, these two tutorials will help you further understand how Node is used to create servers and the nature of callback functions.

As you may imagine, the utility of the above server is quite low, since it has no ability to differentiate between requests and respond in the appropriate fashion.

Assignment

Your objective is to create a server that provides the footballClub data from a JSON file. To accomplish this, you will:

  • use the File System module (fs) to load schools.json into memory
  • create a request handler with the URL module to send the footballClub data on a GET request to localhost:5000/footballClub
  • use the HTTP module to create a server that makes use of this request handler

We have provided skeleton code that will help guide you in completing this assignment.

There is also a file named test.js containing unit tests to test your server once completed. Make sure your server is running on both localhost:5000/footballClub and localhost:5000/coconut before running test.

Instructions:

  1. Make sure you have Node.js installed. Please install the "Current" version and not the "LTS" version.
  2. Clone this repository and then navigate to it on your local machine's terminal See Link for details on how to clone repository - (https://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository)
  3. Use npm install to download all necessary dependencies
  4. Implement the server by filling in code blocks found in server.js and readFile.js, then test your implementation with the command npm test. (make sure your server is running before trying to run the tests!)

Some resources you may find useful:

General Lab Checklist

  1. Make your changes to the lab
  2. Add your new files (if any) with: git add
  3. Commit your changes with: git commit -am “this is what I did”
  4. Upload your changes to github with: git push
  5. Verify that you did things correctly by cloning your own project and testing it with: git clone
  6. Run the automated tests on the cloned version of your project to make sure everything works so that you can get full credit

Grading (25 points)

  1. Load your file into memory and start the server.
  2. Start the server with: npm start
  3. Run the test with: npm test
  4. Check that you can view the file at http://localhost:5000/footballClub
  5. Full credit if the file is viewable and the test passes.
    • (20 points) File is viewable
    • (5 points) Passes automated test

cen3031-fall-lab1's People

Contributors

greyjohnsongit avatar nicksal21 avatar

Watchers

 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.