web server, routing,... Flashcards

1
Q

Creating a simple web server

A

the first step is to include another module, and this one is called http.

First, we create a server with the method createServer ( ).
The method createServer will accept a callback function, which will be fired off each time a new request hits our server. And this callback function gets access to two very important and fundamental variables:
1.It is the request variable : which holds all kinds of stuff like the request url, and a bunch of other stuff.
2.and a response variable : gives us a lot of tools basically for dealing with the response.

💡 In the callback, we execute what we want.

And then second, we start the server so that we can actually listen to incoming requests.
1.in order to do that we actually need to save the result of this createServer here to a new variable. We call it usually server.
2.We call on this server the method listen. Listen accepts a couple of parameters the first one is the port, and second the host.
3.as an optional argument we can also pass in a callback function, which will be run as soon as the server actually starts listening.

💡 When we execute the file, Note that our app keep running; because the whole goal is to wait for the requests to come in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Routing

A

Routing means implemens differents actions for differents URLs.
routing means basically to determine how an application responds to a certain client request, so to a certain URL.
We take decision based on the url, and send back different responses.

💡 We have to implement a response for the situation where we request something that we didn’t handle.(Page not found**)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Status code and headers

A

We can also send Headers: An http header is basically a piece of information about the response that we are sending back.

there are many different standard headers that we can specify to inform the browser or whatever client is receiving a response about the response itself.

💡 We can also specify our own made up headers. So let’s say my-own-header.

💡 Always say to browser what type of content you’re sending back : headers and status code

💡 what matters here is that these headers and also the status code always need to be set before we send out the response.

💡 The request header will set by the browser automatically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

./ and dirname

A

this dot here actually refers to the directory from which we run the node command in the terminal.
This dot here right now represents this current folder here because that is where we actually run the node command, so in this node-farm folder, which is our current working directory anyway in this case.
But we could have run the node command somewhere else, and then the dot would mean something else. So, for example, we could perfectly find, go to the desktop and run node there, and then specify the whole path to index.js, we could do that. but then in this case, the dot would mean the desktop, so if we started the script from the desktop, then this here, this dot, would mean the desktop, and so that is not always ideal.

all Node.js scripts, they get access to a variable called dirname, and that variable always translates to the directory in which the script that we’re currently executing is located.

usually, the dot is where(folder) the script is running, and __dirname is where the current file is located.

💡 Sometimes, they refers to the same place, but it’s a better practice to use the dirname variable.

💡 there is an exception to this rule, which is the require function. So when requiring modules, we can actually require our own modules, and in there, the dot actually also means the current working directory and not the place we’re executing the script from. So just keep in mind that the require function is an exception to this rule.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Our own modules

A

module.exports

exports

require

How well did you know this?
1
Not at all
2
3
4
5
Perfectly