From 36167e55703d18e57540359a99722b0129e2835d Mon Sep 17 00:00:00 2001 From: x-dr Date: Thu, 24 Aug 2023 19:47:18 +0800 Subject: [PATCH] add 404 page --- functions/404.html | 41 +++++++++++++++++++++++++++++++++++++++++ functions/[id].js | 3 ++- functions/create.js | 11 +++++++---- index.html | 2 +- 4 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 functions/404.html diff --git a/functions/404.html b/functions/404.html new file mode 100644 index 0000000..c8f9439 --- /dev/null +++ b/functions/404.html @@ -0,0 +1,41 @@ + + + + + + + + 404 - Page Not Found + + + + +

404 - Page Not Found

+

Sorry, the page you are looking for does not exist.

+

Please check if you have entered the correct URL.

+ + + \ No newline at end of file diff --git a/functions/[id].js b/functions/[id].js index 71312ff..d743d65 100644 --- a/functions/[id].js +++ b/functions/[id].js @@ -1,6 +1,7 @@ /** * @param {string} slug */ +import page404 from './404.html' export async function onRequestGet(context) { const { request, env, params } = context; @@ -27,7 +28,7 @@ export async function onRequestGet(context) { const Url = await env.DB.prepare(`SELECT url FROM links where slug = '${slug}'`).first() if (!Url) { - return new Response("404 Not Found", { + return new Response(page404, { status: 404, headers: { "content-type": "text/html;charset=UTF-8", diff --git a/functions/create.js b/functions/create.js index c38b7b0..1418f62 100644 --- a/functions/create.js +++ b/functions/create.js @@ -43,10 +43,13 @@ export async function onRequestPost(context) { return Response.json({ message: 'Illegal format: url.' }) } - // 自定义slug长度检查 2 10)) { - return Response.json({ message: 'Illegal length: slug, (>= 2 && <= 10).' }) + // 自定义slug长度检查 2 10 || /.+\.[a-zA-Z]+$/.test(slug))) { + return Response.json({ message: 'Illegal length: slug, (>= 2 && <= 10), or not ending with a file extension.' }); } + + + try { @@ -75,7 +78,7 @@ export async function onRequestPost(context) { const bodyUrl = new URL(url); if (bodyUrl.hostname === originurl.hostname) { - return Response.json({ message: 'You cannot shorten a link to the same domain.' },{ + return Response.json({ message: 'You cannot shorten a link to the same domain.' }, { status: 400 }) } diff --git a/index.html b/index.html index cca7bfd..8556421 100644 --- a/index.html +++ b/index.html @@ -210,7 +210,7 @@
自定义设置
- + Slug 默认是随机生成的短 id。