// Import required modules
const http = require('http');
const fs = require('fs');
// Create an HTTP server
const server = http.createServer((req, res) => {
// Check if the request method is 'GET' and the requested URL is '/'
if (req.method === 'GET' && req.url === '/') {
// Read the HTML file
fs.readFile('index.html', (err, data) => {
if (err) {
// If an error occurs, send a 500 Internal Server Error response
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Internal Server Error');
} else {
// Send a 200 OK response with the HTML content
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
}
});
} else {
// If the requested URL is not '/', send a 404 Not Found response
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
}
});
// Listen for incoming requests on port 3000
server.listen(3000, () => {
console.log('Server running on http://localhost:3000/');
});
// Handle 'click' event on the HTML element with the ID 'myButton'
document.getElementById('myButton').onclick = () => {
// Log a message to the console when the button is clicked
console.log('Button clicked!');
};