Skip to content

Instantly share code, notes, and snippets.

@awendt
Created December 1, 2023 08:57
Show Gist options
  • Save awendt/61efdd3d29f3c6eb979ac0c041d578b5 to your computer and use it in GitHub Desktop.
Save awendt/61efdd3d29f3c6eb979ac0c041d578b5 to your computer and use it in GitHub Desktop.
HTTP server returning an error code specified by the client
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