Junior — Middle
Can you explain the main differences between HTTP and HTTPS protocols?
sobes.tech AI
Answer from AI
HTTP (HyperText Transfer Protocol) and HTTPS (HTTP Secure) are protocols for data transfer on the internet, but they have key differences:
- Security: HTTP transmits data in plain text, making it vulnerable to interception and tampering. HTTPS uses SSL/TLS to encrypt data, ensuring confidentiality and integrity.
- Default port: HTTP operates on port 80, HTTPS on port 443.
- Certificates: HTTPS requires a digital certificate to verify the server's authenticity.
Example usage in Node.js:
const http = require('http');
const https = require('https');
// HTTP server
http.createServer((req, res) => {
res.end('Hello over HTTP');
}).listen(80);
// HTTPS server (requires key and certificate)
const fs = require('fs');
const options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};
https.createServer(options, (req, res) => {
res.end('Hello over HTTPS');
}).listen(443);
HTTPS is preferred for protecting user data and increasing trust in a website.