What is a web server?
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.
What are the two parts of every HTTP interaction?
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.
What’s in an HTTP request?
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)
What’s in an HTTP response?
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 do you create a basic web server in Node.js?
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’))
What is routing?
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’)
}
What are parameterized URLs?
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.
What is the Same Origin Policy?
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.
What is CORS and why do you need it?
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 do you receive POST data sent to your server?
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));