const http = require('http'); const server = http.createServer((req, res) => { const url = req.url; if (url === '/') { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Welcome to the Home Page'); } else if (url === '/about') { res.writeHead(200, { 'Content-Type': 'text/html' }); res.end('<h1>About Us</h1><p>This is the about page.</p>'); } else if (url === '/data') { const jsonData = { name: 'Dev', course: 'Node.js', status: 'Learning' }; res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(jsonData)); } else { res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('404 Not Found'); } }); // Start server on port 3000 server.listen(3000, () => { console.log('Server is listening on http://localhost:3000'); });
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter