Challenge - 5 Problems
HTTPS & SSL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when you run this Express HTTPS server code?
Consider this Express server setup using HTTPS. What will the server do when started?
Express
import express from 'express'; import https from 'https'; import fs from 'fs'; const app = express(); app.get('/', (req, res) => { res.send('Secure Hello!'); }); const options = { key: fs.readFileSync('server.key'), cert: fs.readFileSync('server.cert') }; https.createServer(options, app).listen(8443, () => { console.log('HTTPS server running on port 8443'); });
Attempts:
2 left
💡 Hint
Think about how HTTPS servers are created in Node.js with Express.
✗ Incorrect
Using https.createServer with the Express app and valid key and cert files creates a secure HTTPS server. The app responds to requests over HTTPS on the specified port.
📝 Syntax
intermediate1:30remaining
Which option correctly reads SSL certificate files for HTTPS in Express?
You want to create an HTTPS server in Express. Which code snippet correctly reads the SSL key and certificate files?
Attempts:
2 left
💡 Hint
Remember that the HTTPS server needs the actual file contents, not just file names.
✗ Incorrect
fs.readFileSync reads the file contents synchronously, which is required for the HTTPS options. Using fs.readFile is asynchronous and returns a Promise or requires a callback, so it won't work here directly.
🔧 Debug
advanced2:30remaining
Why does this HTTPS Express server crash with 'Error: error:0909006C:PEM routines:get_name:no start line'?
Look at this code snippet. Why does the server crash with the PEM error when starting?
Express
import express from 'express'; import https from 'https'; import fs from 'fs'; const app = express(); const options = { key: fs.readFileSync('server.key'), cert: fs.readFileSync('server.cert') }; https.createServer(options, app).listen(443);
Attempts:
2 left
💡 Hint
PEM errors often relate to how the certificate files are read or formatted.
✗ Incorrect
Reading the key and cert files with 'utf8' encoding returns strings, but Node.js HTTPS expects buffers or raw data. Reading without encoding returns buffers, which is correct.
❓ state_output
advanced2:00remaining
What is the output when accessing the HTTP server that redirects to HTTPS?
Given this Express setup, what response does a client get when requesting http://localhost:3000?
Express
import express from 'express'; const app = express(); app.use((req, res, next) => { if (!req.secure) { return res.redirect('https://' + req.headers.host + req.url); } next(); }); app.get('/', (req, res) => { res.send('Welcome to secure site'); }); app.listen(3000, () => console.log('HTTP server running on port 3000'));
Attempts:
2 left
💡 Hint
Check what happens when req.secure is false in Express middleware.
✗ Incorrect
The middleware checks if the request is not secure (HTTP). If so, it sends a redirect response to the HTTPS URL, causing the client to switch to HTTPS.
🧠 Conceptual
expert3:00remaining
Why is it important to use a trusted SSL certificate in production Express HTTPS servers?
Choose the best reason why production HTTPS servers should use certificates from trusted Certificate Authorities (CAs).
Attempts:
2 left
💡 Hint
Think about user experience and browser security when visiting HTTPS sites.
✗ Incorrect
Browsers show warnings for self-signed or untrusted certificates, which can scare users and reduce trust. Trusted certificates ensure secure, trusted connections.