diff --git a/.gitignore b/.gitignore index 5deaaab..5061a19 100644 --- a/.gitignore +++ b/.gitignore @@ -103,6 +103,8 @@ dist # TernJS port file .tern-port +# VSCode settings +.vscode /data/* !/data/.gitkeep \ No newline at end of file diff --git a/js/index.ts b/js/index.ts index 9f8fff7..7ff4bab 100644 --- a/js/index.ts +++ b/js/index.ts @@ -7,6 +7,8 @@ import { valid } from './util'; const app = express(); +const PORT = 8000; + const ROOT = 'js.bottle.remotehack.space'; const SUBREG = /^(?\w[\w-]*)\.js\.bottle\.remotehack\.space$/; @@ -19,67 +21,62 @@ app.all('/', urlencodedParser, async (req, res, next) => { const {host} = req.headers; - if(host === ROOT) { - // - if(req.method === 'GET') { - res.sendFile(join(__dirname, '/../www/landing.html')) - return; - } - - if(req.method === 'POST') { - - const {subdomain} = req.body + if (host !== ROOT) { + next(); + return; + } - if(valid(subdomain)) { - if(await store.exists({subdomain})) { + if(req.method === 'GET') { + res.sendFile(join(__dirname, '/../www/landing.html')) + return; + } - res.redirect(subdomain + ROOT) - - return; - } else { + if (req.method === 'POST') { - await store.createStore({subdomain}) + const {subdomain} = req.body - res.send(`CREATED SUBDOMAIN: ${req.body.subdomain}`) - return; - } + if (valid(subdomain)) { + if (!await store.exists({subdomain})) { + await store.createStore({subdomain}) } - - return; + res.redirect(`http://${subdomain}.${ROOT}`) + } else { + res.status(404).send(`${subdomain} is not a valid subdomain name!`); } + return; } - - next(); - }) app.all('*', async (req, res) => { const {host} = req.headers; - const match =host?.match(SUBREG); - if(match) { - - const {subdomain} = (match.groups as any) + const match = host?.match(SUBREG); + const {subdomain} = match?.groups || {}; + if (!match || !subdomain) { + const msg = `Failed to match for subdomain on host ${host}`; + console.error(msg); + res.status(404).send(msg); + return; + } - const timestamp = Date.now().toString(); - const content = JSON.stringify([req.headers, req.url]) - + const timestamp = Date.now().toString(); + const content = JSON.stringify([req.headers, req.url]) + try { await store.write({subdomain, timestamp, content}) const result = await store.read({subdomain}); res.contentType('text/plain'); res.send(result); - - return; + } catch (e) { + console.error(e); + res.status(404).send(`Nobody has created this domain yet.`); } - - res.send(`(NODE) Cool `) }) -app.listen(8000, () => { - console.log("started") +app.listen(PORT, () => { + console.log(`started on port ${PORT}`); }); diff --git a/js/tsconfig.json b/js/tsconfig.json index 78095b7..67b7711 100644 --- a/js/tsconfig.json +++ b/js/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { // "module": "ES2020", - "target": "ES2015", + "target": "ES2020", "esModuleInterop": true, "skipLibCheck": true, "moduleResolution": "node",