Concept Flow - Why security is critical
Start: User sends request
Server receives request
Check for security threats
Block
Send response
End
This flow shows how a server checks for security threats before processing a request to keep data safe.
import http from 'node:http'; const server = http.createServer((req, res) => { if (req.headers['x-api-key'] !== 'secret') { res.statusCode = 401; res.end('Unauthorized'); } else { res.end('Welcome!'); } });
| Step | Request Header 'x-api-key' | Condition (key === 'secret') | Action | Response Sent |
|---|---|---|---|---|
| 1 | 'wrongkey' | False | Block request | 'Unauthorized' |
| 2 | 'secret' | True | Process request | 'Welcome!' |
| Variable | Start | After Step 1 | After Step 2 |
|---|---|---|---|
| req.headers['x-api-key'] | undefined | 'wrongkey' | 'secret' |
| res.statusCode | 200 (default) | 401 | 200 (default) |
| res.end message | none | 'Unauthorized' | 'Welcome!' |
Security in Node.js servers means checking requests before processing. Use conditions to block unauthorized access. Example: Check API keys in headers. If check fails, send error response. If check passes, process normally. This protects data and users.