Section 1

Preview this deck

What is useful about the response elements of an HTTP server in node.js?

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 14, 2020

Cards (23)

Section 1

(23 cards)

What is useful about the response elements of an HTTP server in node.js?

Front

Response has methods like writeHead(200, {'Content Type':contentType}); response .end(content, 'utf-8')

Back

The onBLANK syntax for JS eventlisteners.

Front

windwindow.onload = function() { doSomething; }; mySocket.onopen=function(){ console.log("WS connected") }

Back

What does the reponse.end() method of an HTTP server in node.js do?

Front

It will send the response back to the client.

Back

How is the Node.js Server different than our original implementation in Java?

Front

1. The Node.js server has nice methods for parsing HTTP requests in the http module. 2. Node.js is single threaded by default. Pro and con since our server likely loads resources more quickly with multiple threads. 3. Rather than reading bytes from the DataInputStream, in Node.js we have the wsServer.on('request', function(request) callback. This allows us to easily process messages as they come in rather than using a blocking method like read which will block until bytes are available to read. 4. Node.js messages are easy to send via sendUTF(message.utf8Data). The message is retrieved from the callback.

Back

How many threads does node have by default?

Front

Node is single threaded by default.

Back

What is a callback?

Front

A callback is a function that will be executed once a different function finishes executing.

Back

How do you install new dependencies using the NPM?

Front

npm install packagename this works like homebrew.

Back

What is the arguments parameter?

Front

An implicit parameter for js functions that can be used to explore which arguments were passed. The parameter is an array of the arguments passed. Ex arguments.length Ed: arguments[3] //returns 4th argument

Back

How can you explore the number or arguments passed to a JS function?

Front

Use the arguments parameter which is an implicit parameter. Could be useful for logging an argument ex: function argumentVar(parameter1, parameter2, parameter3){ console.log(arguments.length); // Logs the number of arguments passed. console.log(arguments[3]); // Logs the 4th argument. Follows array indexing notations. }

Back

What is the difference between a parameter and an argument in Javascript? Why is this distinction important?

Front

Parameters are variables listed as a part of the function definition. Arguments are values passed to the function when it is invoked. This is important because JavaScript does not throw an error if the number of arguments passed during a function invocation are different than the number of parameters listed during function definition.

Back

How do you implement a callback in JS without using anonymous functions?

Front

Notice the 3rd parameter is an object callback. function add(a, b , callback){ document.write(`The sum of ${a} and ${b} is ${a+b}.` +"<br>"); callback(); //notice whatever function was passed as callback is now called. } Example of a function that could be passed function disp(){ document.write('This must be printed after addition'); } Example of a complete call of the function with a callback. // Calling add() function add(5,6,disp); Output: The sum of 5 and 6 is 11. This must be printed after addition

Back

How can you load a library into a Node project once it is downloaded into the NPM?

Front

Use the require function var http = require('http')

Back

What does the HTTP library in Node allow you to do?

Front

Create an HTTP server. let server = http.createServer(function (request, response) { //read stuff from the request and write stuff to the response here //Basically all the code but the websocket section. }).listen(8080);

Back

What happens if you supply too few arguments to a function in JS?

Front

It will execute anyways and set any parameters that do not have corresponding arguments to undefined. function argCheck(parameter1, parameter2, parameter3){ console.log(parameter1 + parameter2 + parameter3);} // Function with missing arguments argCheck(1,2); // Logs NaN because by default if a corresponding argument is missing, it is set to undefined. // parameter3 is assigned undefined and so 1+2+undefined = NaN

Back

How can you read a file in node.js and send it back to a client?

Front

Use the fs class. fs.readFile('resources/index.html',function(error,content)

Back

What type of data structure is the mime-types variable in our Node.js file and what is it's function?

Front

It is a map of filenames (Ex .html) with types of those files (Ex: text/html) It allows us to store the contentType in a variable by calling mimetypes[.html] //The key can be whatever the last bit of the filename is. This is important for writing the head and sending it: response.writeHead(200, { 'Content-Type': contentType }); response.end(content, 'utf-8');

Back

How could you implement a callback in JS with an anonymous function?

Front

add(5,6,function disp(){ document.write('This must be printed after addition.'); }); NOTE THAT THE IMPLEMENTATION OF add(5,6,callback) is elsewhere. We are implementing disp on the fly.

Back

What does the full read and send files thing look like in Node.js?

Front

Back

What happens if you supply too many arguments to a function in JS?

Front

It will ignore extra arguments that are beyond the number of parameters listed in the function definition. Ex: function argCheck(parameter1, parameter2, parameter3){ console.log(parameter1 + parameter2 + parameter3);} // Function called with extra arguments argCheck(1,2,3,4);// Logs 6 (1 + 2 + 3, ignores 4)

Back

What is useful about the request element of an HTTP server in node.js?

Front

There are useful fields within the request which will allow us to parse it more easily. Ex. filepath = request.url; //No need to parse out the GET and everything else.

Back

Callback syntax for node.js receiving messages

Front

connection.on('message',function(message) { if (message.type === 'utf8'){ console.log(message.utf8Data)

Back

What is Node Package Manager (NPM)?

Front

It is the node equivalent of Gradle which automatically downloads requested packages from a central location and installs the library so that you can use it.

Back

Why are callbacks important?

Front

Because JavaScript is an Event Driven language, rather than waiting for a single response before moving on. JavaScript will keep executing while listening for other events. Callbacks are a way to make sure certain code doesn't execute until other code has already finished execution.

Back