Created
March 17, 2025 14:46
-
-
Save devtdeng/772f284836f6c2c1571868445691f15f to your computer and use it in GitHub Desktop.
nodejs sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import built-in Node.js libraries | |
const http = require('http'); | |
const { setTimeout } = require('timers/promises'); // Used to set a delay | |
// Create the server | |
const server = http.createServer(async (req, res) => { | |
// Check if it's a GET request | |
if (req.method === 'GET' && req.url === '/') { | |
// Wait for 5 seconds | |
await setTimeout(5000); | |
// Send the response after 5 seconds | |
res.statusCode = 200; | |
res.setHeader('Content-Type', 'text/plain'); | |
res.end('Hello, World! The request was processed after waiting for 5 seconds.'); | |
} else { | |
// Handle other requests | |
res.statusCode = 404; | |
res.end('Not Found'); | |
} | |
}); | |
// Define the port | |
const PORT = process.env.PORT || 3000; | |
// Start the server | |
server.listen(PORT, () => { | |
console.log(`Server is running on http://localhost:${PORT}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment