Skip to content

Instantly share code, notes, and snippets.

@swuecho
Last active April 9, 2021 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swuecho/8030b586f83cd4efb78719c7ece04004 to your computer and use it in GitHub Desktop.
Save swuecho/8030b586f83cd4efb78719c7ece04004 to your computer and use it in GitHub Desktop.
demo_deno.ts
function handleRequest(request) {
const { pathname } = new URL(request.url);
// Respond with HTML
if (pathname.startsWith("/html")) {
const html = `<html>
<p><b>Message:</b> Hello from Deno Deploy.</p>
</html>`;
return new Response(html, {
headers: {
// The "text/html" part implies to the client that the content is HTML
// and the "charset=UTF-8" part implies to the client that the content
// is encoded using UTF-8.
"content-type": "text/html; charset=UTF-8",
},
});
}
// Respond with JSON
if (pathname.startsWith("/json")) {
// Use stringify function to convert javascript object to JSON string.
const json = JSON.stringify([{"id": 1, "choice": "湖北省", "question": "省份"},{"id": 2, "choice": "宁夏回族自治区", "question": "省份"},{"id": 3, "choice": "上海市", "question": "省份"},{"id": 4, "choice": "辽宁省", "question": "省份"},{"id": 5, "choice": "云南省", "question": "省份"},{"id": 6, "choice": "福建省", "question": "省份"},{"id": 7, "choice": "山西省", "question": "省份"},{"id": 8, "choice": "海南省", "question": "省份"},{"id": 9, "choice": "吉林省", "question": "省份"},{"id": 10, "choice": "广东省", "question": "省份"},{"id": 11, "choice": "山东省", "question": "省份"},{"id": 12, "choice": "浙江省", "question": "省份"},{"id": 13, "choice": "江苏省", "question": "省份"},{"id": 14, "choice": "甘肃省", "question": "省份"},{"id": 15, "choice": "广西壮族自治区", "question": "省份"},{"id": 16, "choice": "西藏自治区", "question": "省份"},{"id": 17, "choice": "黑龙江省", "question": "省份"},{"id": 18, "choice": "北京市", "question": "省份"},{"id": 19, "choice": "内蒙古自治区", "question": "省份"},{"id": 20, "choice": "青海省", "question": "省份"},{"id": 21, "choice": "新疆维吾尔自治区", "question": "省份"},{"id": 22, "choice": "陕西省", "question": "省份"},{"id": 23, "choice": "湖南省", "question": "省份"},{"id": 24, "choice": "江西省", "question": "省份"},{"id": 25, "choice": "河北省", "question": "省份"},{"id": 26, "choice": "河南省", "question": "省份"},{"id": 27, "choice": "贵州省", "question": "省份"},{"id": 28, "choice": "天津市", "question": "省份"},{"id": 29, "choice": "四川省", "question": "省份"},{"id": 30, "choice": "安徽省", "question": "省份"},{"id": 31, "choice": "重庆市", "question": "省份"}]);
return new Response(json, {
headers: {
'Access-Control-Allow-Origin': 'https://www.bestqa.net',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, PUT, PATCH, DELETE',
'Access-Control-Allow-Headers': 'X-Requested-With,content-type',
'Access-Control-Allow-Credentials':true,
"content-type": "application/json; charset=UTF-8",
},
});
}
}
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment