Create a http server listening request at port 3000. Process the request to provide different type of resources as response. (HTML, TEXT, JSON, etc.).
Tue Apr 22 2025 18:38:07 GMT+0000 (Coordinated Universal Time)
Saved by
@fsd
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');
});
content_copyCOPY
Comments