Server Flashcards

1
Q

How to create my own server?

A

const http = require(‘http’);
const hostname = ‘localhost’;
const port = 8000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader(‘Content-Type’, ‘text/plain’);
res.end(‘Hello, world!\n’);
});

server.listen(port, hostname, () => {
console.log(Server running at http://${hostname}:${port}/);
});

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

Explain this code.
const http = require(‘http’)

A

This line of code is using Node.js’s built-in require() function to import the http module.

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

What are modules in node js?

A

Modules are collections of functions and objects that you can use in your code.

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

what does http module allows you to do?

A
  1. Create an HTTP server: You can create an HTTP server using the http.createServer() method.
  2. Handle HTTP requests: Inside the callback function passed to createServer(), you can write logic to handle HTTP requests. This includes processing incoming data, routing requests to the appropriate handlers, and generating responses.
  3. Send HTTP responses: When you receive an HTTP request, you need to send an appropriate HTTP response back to the client. You can do this using methods like response.writeHead() and response.end()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain the syntax in js
( ) => { … }

A

Define an arrow function (concise way to write functions) in JavaScript.

Parentheses ( ): For enclosing the parameters of the function.

Arrow =>: Separates the parameters from the function body.

Curly braces { … }: Contain the body of the function, which contains the code that will be executed when the function is called.

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

What kind of a function is greet?
Explain this code

const greet = () => {
console.log(“Hello!”);
}

A

greet (constant) is assigned as an arrow function (arrow function is different from regular function; usually arrow function more limited but better for conciseness)
When the greet function is called, it simply logs “Hello!” to the console.

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

What does it mean to create my own server?

A

Making your computer available to provide services to other computers on the network.

Here’s a simplified breakdown:

  1. Listening for requests: Your server program waits and listens for requests that come in over the network.
  2. Handling requests: When your server receives a request, it processes that request according to its programming.
  3. Sending responses: After processing the request, your server sends back a response. This response could be HTML for a webpage, JSON data for an API request, or even just a simple confirmation message.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

http: This is the core module in Node.js for creating HTTP servers and clients. It’s the foundation for building web applications.
fs: This module stands for “File System”. It provides methods for interacting with the file system on your computer. Although not strictly necessary for creating a basic server, it’s often included for scenarios where you might want to read or write files in response to HTTP requests. In this case, it might be used to serve static files like HTML, CSS, or images.
url: This module is used for URL resolution and parsing. It helps in parsing URLs into their various components (protocol, hostname, pathname, query parameters, etc.). While you can create a basic server without this, it’s useful when you want to handle different routes or parameters in your URL.
querystring: This module is used for parsing and formatting URL query strings. It’s helpful when dealing with query parameters in URLs. Like url, it’s not strictly necessary for a basic server, but it’s useful when you need to handle query parameters in HTTP requests.

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

What is url parsing?

A

Process of breaking down a URL (Uniform Resource Locator) into its constituent parts, such as the protocol, hostname, port, path, query parameters, and fragment identifier. This parsing allows you to work with the various components of a URL separately.

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

Parse this url

https://www.example.com:8080/path/to/resource?param1=value1&param2=value2#section3

A

Protocol: https
Hostname: www.example.com
Port: 8080 (if specified)
Path: /path/to/resource
Query Parameters: { param1: ‘value1’, param2: ‘value2’ }
Fragment Identifier: section3

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