Created
December 2, 2024 13:51
-
-
Save Bjorn-Eric-Abr/29668c5fb2c5a09dbb6a2832ce062965 to your computer and use it in GitHub Desktop.
Node Instant Repeater 99
This file contains hidden or 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
| const http = require("http"); | |
| const url = require("url"); | |
| const server = http.createServer((req, res) => { | |
| const parsedUrl = url.parse(req.url); | |
| const date = new Date(); | |
| const time = date.toLocaleTimeString("sv-SE", { hour12: false }); | |
| // Handle OPTIONS requests | |
| if (req.method === "OPTIONS") { | |
| console.log(time, ":", "Options"); | |
| res.writeHead(200, { | |
| "Access-Control-Allow-Origin": "*", | |
| "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", | |
| "Access-Control-Allow-Headers": "*", | |
| "Access-Control-Max-Age": "86400", | |
| }); | |
| res.end(); | |
| return; | |
| } | |
| res.writeHead(405, { | |
| "Content-Type": "application/json", | |
| Connection: "keep-alive", | |
| Accept: "application/x-protobuf", | |
| "Access-Control-Allow-Origin": "*", | |
| "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", | |
| "Access-Control-Allow-Headers": "*", | |
| "Access-Control-Max-Age": "86400", | |
| Allow: "GET", | |
| }); | |
| const errorResponse = { | |
| error: "Method Not Allowed", | |
| message: `${req.method} is not allowed for this resource`, | |
| request: { | |
| path: parsedUrl.pathname, | |
| httpVersion: req.httpVersion, | |
| }, | |
| status: 405, | |
| }; | |
| console.log(time, ":", JSON.stringify(errorResponse, null, 2)); | |
| res.end(JSON.stringify(errorResponse, null, 2)); | |
| console.log(); | |
| }); | |
| const PORT = 3011; | |
| server.listen(PORT, () => { | |
| console.log(`Server running at http://localhost:${PORT}`); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment