Create a Http server listening on port 3000. Process the request to provide different types of resources as response(Html,json,text,js)
Mon Apr 07 2025 23:56:12 GMT+0000 (Coordinated Universal Time)
Saved by
@p9876543
const http = require('http');
const server = http.createServer((req, res) => {
const { url } = req;
if (url === '/html') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Welcome to the HTML response</h1>');
} else if (url === '/json') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'This is a JSON response', status: 'success' }));
} else if (url === '/text') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('This is a plain text response.');
} else if (url === '/js') {
res.writeHead(200, { 'Content-Type': 'application/javascript' });
res.end('console.log("JavaScript response from server");');
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Resource not found');
}
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000');
});
content_copyCOPY
Comments