Skip to content

Instantly share code, notes, and snippets.

@ShuJun-Junical
Last active July 8, 2021 12:50
Show Gist options
  • Save ShuJun-Junical/25b85d357e40b15bbdd9d04dd9d57524 to your computer and use it in GitHub Desktop.
Save ShuJun-Junical/25b85d357e40b15bbdd9d04dd9d57524 to your computer and use it in GitHub Desktop.
云函数:通过JSDelivr代理访问静态站
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
}
}
}
}
{
"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"
}
}
@ShuJun-Junical
Copy link
Author

  • 修改第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