Skip to content

Commit

Permalink
Integrating Cloudflare Pages Function as backend (cf-pages#1)
Browse files Browse the repository at this point in the history
* update

* Use Cloudflare Page Function as Backend, no need to deploy Cloudflare Workers separately.
  • Loading branch information
xyTom authored Oct 31, 2022
1 parent d00a874 commit ae7db67
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package-lock.json
_nuxt/b46e75617207ae5013d0.js
node_modules
package.json
2 changes: 1 addition & 1 deletion _nuxt/b46e75617207ae5013d0.js

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions functions/api/bing/wallpaper/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export async function onRequest(context) {
// Contents of context object
const {
request, // same as existing Worker API
env, // same as existing Worker API
params, // if filename includes [id] or [[path]]
waitUntil, // same as ctx.waitUntil in existing Worker API
next, // used for middleware or to fetch assets
data, // arbitrary space for passing data between middlewares
} = context;
const res = await fetch(`https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=5`);
const bing_data = await res.json();
const return_data={
"status":true,
"message":"操作成功",
"data": bing_data.images
}
const info = JSON.stringify(return_data);
return new Response(info);

}
19 changes: 19 additions & 0 deletions functions/file/[id].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export async function onRequest(context) { // Contents of context object
const {
request, // same as existing Worker API
env, // same as existing Worker API
params, // if filename includes [id] or [[path]]
waitUntil, // same as ctx.waitUntil in existing Worker API
next, // used for middleware or to fetch assets
data, // arbitrary space for passing data between middlewares
} = context;
context.request
const url = new URL(request.url);
const response = fetch('https://telegra.ph/' + url.pathname + url.search, {
method: request.method,
headers: request.headers,
body: request.body,
});
return response;
}

19 changes: 19 additions & 0 deletions functions/upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export async function onRequestPost(context) { // Contents of context object
const {
request, // same as existing Worker API
env, // same as existing Worker API
params, // if filename includes [id] or [[path]]
waitUntil, // same as ctx.waitUntil in existing Worker API
next, // used for middleware or to fetch assets
data, // arbitrary space for passing data between middlewares
} = context;
context.request
const url = new URL(request.url);
const response = fetch('https://telegra.ph/' + url.pathname + url.search, {
method: request.method,
headers: request.headers,
body: request.body,
});
return response;
}

0 comments on commit ae7db67

Please sign in to comment.