Last active
July 8, 2021 12:50
-
-
Save ShuJun-Junical/25b85d357e40b15bbdd9d04dd9d57524 to your computer and use it in GitHub Desktop.
云函数:通过JSDelivr代理访问静态站
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require("axios"); | |
exports.main_handler = async (event, context, callback) => { | |
const baseUrl = "https://cdn.jsdelivr.net/gh/HZ-Geek/hz-geek_blog@master/dist"; | |
let path = event.path; | |
if (path.indexOf('.') != -1) { | |
return { | |
isBase64Encoded: false, | |
statusCode: 301, | |
headers: { 'Location': baseUrl + path }, | |
} | |
} | |
else { | |
try { | |
let resopnse = await axios.get(baseUrl + '/index.html'); | |
return { | |
isBase64Encoded: false, | |
statusCode: 200, | |
headers: { 'Content-Type': 'text/html' }, | |
body: resopnse.data | |
} | |
} | |
catch (error) { | |
return { | |
isBase64Encoded: false, | |
statusCode: 200, | |
headers: { 'Content-Type': 'text/html' }, | |
body: error | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "hzgeek-blog-proxy", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "JupiterJun", | |
"license": "MIT", | |
"dependencies": { | |
"axios": "^0.21.1" | |
} | |
} |
Author
ShuJun-Junical
commented
May 22, 2021
- 修改第4行baseUrl为自己网站的JSDelivr的根目录
- 需要腾讯云函数,其他云函数提供商移植可适当修改代码
- 需要Axios依赖包(v0.21.1 可用)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment