Created
December 1, 2023 08:57
-
-
Save awendt/61efdd3d29f3c6eb979ac0c041d578b5 to your computer and use it in GitHub Desktop.
HTTP server returning an error code specified by the client
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 * as http from "node:http"; | |
const PORT = 8000; | |
// source: https://developer.mozilla.org/en-US/docs/Learn/Server-side/Node_server_without_framework | |
http | |
.createServer(async (req, res) => { | |
const statusCode = parseInt(req.url.slice(1)); | |
res.writeHead(statusCode, { "Content-Type": "application/json" }); | |
res.end("{}"); | |
console.log(`${req.method} ${req.url} ${statusCode}`); | |
}) | |
.listen(PORT); | |
console.log(`Server running at http://127.0.0.1:${PORT}/`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment