Skip to content

Commit

Permalink
fix:跨域
Browse files Browse the repository at this point in the history
  • Loading branch information
x-dr committed Feb 7, 2024
1 parent 36167e5 commit 660a81d
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions functions/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,31 @@ export async function onRequestPost(context) {
const timedata = new Date();
const formattedDate = new Intl.DateTimeFormat('zh-CN', options).format(timedata);
const { url, slug } = await request.json();

const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '86400', // 24 hours
};
if (!url) return Response.json({ message: 'Missing required parameter: url.' });

// url格式检查
if (!/^https?:\/\/.{3,}/.test(url)) {
return Response.json({ message: 'Illegal format: url.' })
return Response.json({ message: 'Illegal format: url.' },{
headers: corsHeaders,
status: 400
})
}

// 自定义slug长度检查 2<slug<10 是否不以文件后缀结尾
if (slug && (slug.length < 2 || slug.length > 10 || /.+\.[a-zA-Z]+$/.test(slug))) {
return Response.json({ message: 'Illegal length: slug, (>= 2 && <= 10), or not ending with a file extension.' });
return Response.json({ message: 'Illegal length: slug, (>= 2 && <= 10), or not ending with a file extension.' },{
headers: corsHeaders,
status: 400

});
}




try {
Expand All @@ -59,12 +70,18 @@ export async function onRequestPost(context) {

// url & slug 是一样的。
if (existUrl && existUrl.existUrl === url) {
return Response.json({ slug, link: `${origin}/${slug2}` })
return Response.json({ slug, link: `${origin}/${slug2}` },{
headers: corsHeaders,
status: 200
})
}

// slug 已存在
if (existUrl) {
return Response.json({ message: 'Slug already exists.' })
return Response.json({ message: 'Slug already exists.' },{
headers: corsHeaders,
status: 200
})
}
}

Expand All @@ -73,12 +90,17 @@ export async function onRequestPost(context) {

// url 存在且没有自定义 slug
if (existSlug && !slug) {
return Response.json({ slug: existSlug.existSlug, link: `${origin}/${existSlug.existSlug}` })
return Response.json({ slug: existSlug.existSlug, link: `${origin}/${existSlug.existSlug}` },{
headers: corsHeaders,
status: 200

})
}
const bodyUrl = new URL(url);

if (bodyUrl.hostname === originurl.hostname) {
return Response.json({ message: 'You cannot shorten a link to the same domain.' }, {
headers: corsHeaders,
status: 400
})
}
Expand All @@ -90,10 +112,16 @@ export async function onRequestPost(context) {
const info = await env.DB.prepare(`INSERT INTO links (url, slug, ip, status, ua, create_time)
VALUES ('${url}', '${slug2}', '${clientIP}',1, '${userAgent}', '${formattedDate}')`).run()

return Response.json({ slug: slug2, link: `${origin}/${slug2}` })
return Response.json({ slug: slug2, link: `${origin}/${slug2}` },{
headers: corsHeaders,
status: 200
})
} catch (e) {
// console.log(e);
return Response.json({ message: e.message })
return Response.json({ message: e.message },{
headers: corsHeaders,
status: 500
})
}


Expand Down

0 comments on commit 660a81d

Please sign in to comment.