From 20aca1b4bba6d8dbe0b549efabcf7a01b026c63b Mon Sep 17 00:00:00 2001 From: Mhykol Date: Thu, 16 May 2024 02:46:23 -0400 Subject: Improved handling for favicon.ico and added robots.txt --- app.js | 22 ++++++++++++++++------ assets/img/favicon.ico | Bin 205086 -> 0 bytes assets/robots/robots.txt | 2 ++ source/readfile.js | 22 +++++++++++++++++++++- 4 files changed, 39 insertions(+), 7 deletions(-) delete mode 100644 assets/img/favicon.ico create mode 100644 assets/robots/robots.txt diff --git a/app.js b/app.js index 74cb1f3..985e66a 100644 --- a/app.js +++ b/app.js @@ -19,6 +19,7 @@ const mime = { png: 'image/png', webp: 'image/webp', avif: 'image/avif', + ico: 'image/ico', svg: 'image/svg+xml', js: 'application/javascript' } @@ -57,12 +58,21 @@ class App { string: req.path } - if (this.path.split[1] === 'favicon.ico') { - const data = this.readfile.GetFile('/img/favicon.ico') - data.mime.then(output => this.#FileOpen({data: data, mime: output, res: res})) - } else { - if (this.path.string.endsWith('/') && this.path.string.length > 1) this.path.string = this.path.string.substring(0, this.path.string.length - 1) - if (output.has(this.path.string)) {res.send(this.readfile.GetMain())} else {res.sendStatus(404)} + switch (this.path.split[1]) { + case 'favicon.ico': + this.data = this.readfile.GetFavicon() + this.data.mime.then(output => this.#FileOpen({data: this.data, mime: output, res: res})) + break + case 'robots.txt': + console.log('hit') + this.data = this.readfile.GetRobots() + this.data.mime.then(output => this.#FileOpen({data: this.data, mime: output, res: res})) + break + default: + if (this.path.string.endsWith('/') && this.path.string.length > 1) this.path.string = this.path.string.substring(0, this.path.string.length - 1) + if (output.has(this.path.string)) {res.send(this.readfile.GetMain())} else {res.sendStatus(404)} + break + } }) }) diff --git a/assets/img/favicon.ico b/assets/img/favicon.ico deleted file mode 100644 index f4ba292..0000000 Binary files a/assets/img/favicon.ico and /dev/null differ diff --git a/assets/robots/robots.txt b/assets/robots/robots.txt new file mode 100644 index 0000000..eb05362 --- /dev/null +++ b/assets/robots/robots.txt @@ -0,0 +1,2 @@ +User-agent: * +Disallow: diff --git a/source/readfile.js b/source/readfile.js index 6d66210..4627781 100644 --- a/source/readfile.js +++ b/source/readfile.js @@ -77,7 +77,27 @@ class ReadFile { }), mime: this.#GetFileType(path).then(output => {return output}) } - if (object.file) {return object} else {return false} + if (object.file) {return object} else {return null} + } + GetFavicon() { + const object = { + file: fs.createReadStream(`./assets/favicon/favicon.ico`).on('error', (err) => { + logger.Error(`${err.code}: Failed to get 'favicon.ico'`) + return null + }), + mime: this.#GetFileType('favicon.ico') + } + if (object.file) {return object} else {return null} + } + GetRobots() { + const object = { + file: fs.createReadStream(`./assets/robots/robots.txt`).on('error', (err) => { + logger.Error(`${err.code}: Failed to get 'robots.txt'`) + return null + }), + mime: this.#GetFileType('robots.txt') + } + if (object.file) {return object} else {return null} } CreateBlog(x) { const data = { -- cgit v1.2.3-70-g09d2