mapcomplete/scripts/legacyRedirector.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

46 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-12-31 14:20:51 +01:00
import * as http from "node:http"
/**
* Redirect people from
* "mapcomplete.osm.be/path?query=parameter#id" to "mapcomplete.org/path?query=parameter#id"
*/
2023-12-31 14:39:16 +01:00
const PORT = 1236
2023-12-31 14:20:51 +01:00
const CORS = "http://localhost:1234,https://mapcomplete.org,https://dev.mapcomplete.org"
async function redirect(req: http.IncomingMessage, res: http.ServerResponse) {
try {
console.log(
req.method + " " + req.url,
"from:",
req.headers.origin,
new Date().toISOString()
)
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
)
res.setHeader("Access-Control-Allow-Origin", req.headers.origin ?? "*")
if (req.method === "OPTIONS") {
res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, UPDATE")
res.writeHead(204, { "Content-Type": "text/html" })
res.end()
return
}
console.log("Request url:", req.url)
const oldUrl = new URL("https://127.0.0.1:8080" + req.url)
const newUrl = "https://mapcomplete.org" + oldUrl.pathname + oldUrl.search + oldUrl.hash
res.writeHead(301, { "Content-Type": "text/html", Location: newUrl })
res.write("Moved permantently")
res.end()
} catch (e) {
console.error(e)
}
}
http.createServer(redirect).listen(PORT)
console.log(
`Server started at http://127.0.0.1:${PORT}/, the time is ${new Date().toISOString()}, version from package.json is`
)