Wednesday, September 7, 2016

Notes on Video # 13

Video Notes:

Video 13 of Node JS Tutorial, thenewboston
Video Name: Creating a Basic Server

- an http server
- javascript was made for the web browser
- node js = code that will be on a server, will make a request for information from the database, the server will look at the request, and send information back in response to the request
- call the http core module (built into nodejs)
- then write "http.createServer"
- server has to be listening for request
- whenever a user uses the server, we need to call a function such as "onRequest", therefore, you have to build this function
- needs two parameters
- one parameter is the request object - information about user's request, what info were they trying to get, etc.
- second parameter is response - object/data we can send back to them
- there are different status codes that signify whether the request was successful or not, etc.
- use "request.writeHead", has two parameters, status code, and header information = what type of data you are sending back to them
- whenever a user connects to your server, the browser sends a request looking for the page, and makes another request for the favicon

Code created by tutorial maker:
var http = require('http');

function onRequest(request, response){
  console.log("A user made a request" + request.url);
  response.writeHead(200, [Context-Type]: "text/plain");
  response.write("Here is some data");
  response.end();
}

http.createServer(onRequest).listen(8888);
console.log("Server is now running....");


No comments:

Post a Comment