Web servers Flashcards

(10 cards)

1
Q

What is a web server?

A

A program that listens for requests from clients (browsers, apps) and sends back responses. When you visit a website, your browser sends a request — the web server sends back the page.

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

What are the two parts of every HTTP interaction?

A

A request (sent by the client — what they want) and a response (sent by the server — what they get back). Every HTTP interaction is always this request/response pair.

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

What’s in an HTTP request?

A

Four key things:

Method — what action (GET, POST, PUT, DELETE)

Path — where (/users, /products/1)

Headers — extra info (content type, auth token)

Body — data being sent (only on POST/PUT)

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

What’s in an HTTP response?

A

Three key things:

Status code — did it work? (200 OK, 404 Not Found, 500 Server Error)

Headers — extra info about the response

Body — the actual data sent back (HTML, JSON, etc.)

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

How do you create a basic web server in Node.js?

A

const http = require(‘http’)

const server = http.createServer((req, res) => {
res.writeHead(200, { ‘Content-Type’: ‘text/plain’ })
res.end(‘Hello World’)
})

server.listen(3000, () => console.log(‘Server running on port 3000’))

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

What is routing?

A

Deciding what to do based on the URL path. You check req.url and req.method to send back different responses for different routes:

if (req.method === ‘GET’ && req.url === ‘/users’) {
res.end(‘Here are the users’)
}

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

What are parameterized URLs?

A

URLs with dynamic values in the path — like /users/42 where 42 is the user ID. The number can change but the route pattern stays the same: /users/:id.

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

What is the Same Origin Policy?

A

A browser security rule that blocks a web page from making requests to a different domain than the one it was loaded from. Example: a page on mysite.com can’t fetch data from othersite.com by default.

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

What is CORS and why do you need it?

A

Cross-Origin Resource Sharing — a way to lift the Same Origin Policy for specific trusted origins. Your server adds headers to tell the browser “it’s ok, this other domain is allowed to talk to me”:

res.setHeader(‘Access-Control-Allow-Origin’, ‘*’)

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

How do you receive POST data sent to your server?

A

POST data arrives as a stream — you collect the chunks and parse them when done:

let body = ‘’;

req.on(‘data’, chunk => body += chunk.toString());
req.on(‘end’, () => {
try {
const data = JSON.parse(body);
console.log(data);
} catch (err) {
console.error(‘Invalid JSON:’, err.message);
}
});
req.on(‘error’, err => console.error(‘Stream error:’, err.message));

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