前言
支持跨域、随意开关接口的 hexo 博客 api 插件hexo-info-api。
响应头
| 12
 3
 4
 5
 
 | Content-Type: application/json; charset=utf-8
 Access-Control-Allow-Origin: <config.allowOrigin>
 
 Access-Control-Allow-Credentials: true
 
 | 
数据将以 UTF-8 编码 json 格式返回
你可以从配置文件设置允许跨域访问的域名(设置为 * 为允许所有域名)
安装
| 12
 3
 4
 5
 
 | npm install hexo-info-api --save
 
 
 yarn add hexo-info-api
 
 | 
配置
向 hexo 的配置文件 _config.yml 中添加以下内容
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 
 | hexo_info_api:allowOrigin: "*"
 enable:
 - getInfo
 - getPostCount
 - getPosts
 - getPostsByCategory
 - getPostsByCategoryId
 - getPostsByTag
 - getPostsByTagId
 - getPostByPath
 - getPostById
 - getCategories
 - getTags
 
 
 disable_default_api: false
 
 | 
运行 (测试)
返回结果
这是 GET /api/getPostCount 接口的返回结果
| 12
 3
 4
 5
 6
 
 | {"type": "getPostCount",
 "data": {
 "count": 0
 }
 }
 
 | 
接口说明
- 默认接口
 GET /api/ - 获取当前启用的 API 接口列表
例子:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | {"type": "apiInfo",
 "data": {
 "enabled": [
 "getInfo",
 "getPostCount",
 "getPosts",
 "getPostsByCategory",
 "getPostsByCategoryId",
 "getPostsByTag",
 "getPostsByTagId",
 "getPostByPath",
 "getPostById",
 "getCategories",
 "getTags"
 ]
 }
 }
 
 | 
- 获取网站信息
 GET /api/getInfo/ - 获取当前网站的信息(数据来源与 _config.yml)
例子:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | {"type": "getInfo",
 "data": {
 "title": "Hexo中文测试",
 "subtitle": "",
 "description": "",
 "author": "John Doe",
 "language": "en",
 "timezone": "",
 "url": "http://example.com"
 }
 }
 
 | 
- 获取文章数量
 GET /api/getPostCount/ - 获取当前网站的文章数量
例子:
| 12
 3
 4
 5
 6
 
 | {"type": "getPostCount",
 "data": {
 "count": 10
 }
 }
 
 | 
- 获取文章列表
 GET /api/getPosts/ - 获取当前网站的文章列表(所有文章)。返回数据.data.posts: Array
例子:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | {title: String,
 date: String,
 description: String,
 categories: Array,
 tags: Array,
 _path: String,
 _link: String,
 _id: String
 }
 
 | 
例子:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 
 | {"type": "getPosts",
 "data": {
 "posts": [
 {
 "title": "1",
 "date": "2022-09-06T01:24:03.000Z",
 "description": "",
 "categories": [
 { "name": "test1", "_id": "cl7ps65yb0000x88d98gzczh3" },
 { "name": "test2", "_id": "cl7ps65yd0003x88d7518aszd" }
 ],
 "tags": [
 { "name": "test_tag1", "_id": "cl7ps65yc0001x88d71rr08su" },
 { "name": "test_tag2", "_id": "cl7ps65yc0002x88d2kvegq0o" },
 { "name": "test_tag3", "_id": "cl7ps65yd0004x88db7q32te3" }
 ],
 "_path": "2022/09/06/1/",
 "_link": "http://example.com/2022/09/06/1/",
 "_id": "cl7pldj300000p08detj8etxb"
 },
 {
 "title": "2",
 "date": "2022-09-06T01:24:04.000Z",
 "description": "",
 "categories": [],
 "tags": [],
 "_path": "2022/09/06/2/",
 "_link": "http://example.com/2022/09/06/2/",
 "_id": "cl7pldj340001p08d6o7a7z1w"
 },
 {
 "title": "Hello World",
 "date": "2022-09-06T01:22:20.002Z",
 "description": "",
 "categories": [],
 "tags": [],
 "_path": "2022/09/06/hello-world/",
 "_link": "http://example.com/2022/09/06/hello-world/",
 "_id": "cl7pldj3h0009p08dcmvx29lk"
 }
 ]
 }
 }
 
 | 
- 获取分类下的文章列表
 GET /api/getPostsByCategory/:category - 获取当前网站的文章列表(指定分类下的文章)
例子:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 
 | {"type": "getPostsByCategory",
 "data": [
 {
 "title": "1",
 "date": "2022-09-06T01:24:03.000Z",
 "description": "",
 "categories": [
 { "name": "test1", "_id": "cl7ps65yb0000x88d98gzczh3" },
 { "name": "test2", "_id": "cl7ps65yd0003x88d7518aszd" }
 ],
 "tags": [
 { "name": "test_tag1", "_id": "cl7ps65yc0001x88d71rr08su" },
 { "name": "test_tag2", "_id": "cl7ps65yc0002x88d2kvegq0o" },
 { "name": "test_tag3", "_id": "cl7ps65yd0004x88db7q32te3" }
 ],
 "_path": "2022/09/06/1/",
 "_link": "http://example.com/2022/09/06/1/",
 "_id": "cl7pldj300000p08detj8etxb"
 }
 ]
 }
 
 | 
- 获取分类 ID 下的文章列表
 GET /api/getPostsByCategoryId/:categoryId - 获取当前网站的文章列表(指定分类ID下的文章)
例子: (与 获取分类下的文章列表 返回的数据相同)
- 获取标签下的文章列表
 GET /api/getPostsByTag/:tag - 获取当前网站的文章列表(指定标签下的文章)
例子:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 
 | {"type": "getPostsByTag",
 "data": [
 {
 "title": "1",
 "date": "2022-09-06T01:24:03.000Z",
 "description": "",
 "categories": [
 { "name": "test1", "_id": "cl7ps65yb0000x88d98gzczh3" },
 { "name": "test2", "_id": "cl7ps65yd0003x88d7518aszd" }
 ],
 "tags": [
 { "name": "test_tag1", "_id": "cl7ps65yc0001x88d71rr08su" },
 { "name": "test_tag2", "_id": "cl7ps65yc0002x88d2kvegq0o" },
 { "name": "test_tag3", "_id": "cl7ps65yd0004x88db7q32te3" }
 ],
 "_path": "2022/09/06/1/",
 "_link": "http://example.com/2022/09/06/1/",
 "_id": "cl7pldj300000p08detj8etxb"
 }
 ]
 }
 
 | 
- 获取标签 ID 下的文章列表
 GET /api/getPostsByTagId/:tagId - 获取当前网站的文章列表(指定标签ID下的文章)
例子: (与获取标签下的文章列表相同)
- 获取指定路径的文章数据
 GET /api/getPostByPath/:path - 获取指定路径的文章数据
例子: api/getPostByPath/yyyy/mm/dd/hello-world/
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | {"type": "getPostByPath",
 "data": {
 "title": "Hello World",
 "date": "2022-09-06T01:22:20.002Z",
 "description": "",
 "categories": [],
 "tags": [],
 "content": "...html document...",
 "_id": "cl7pldj3h0009p08dcmvx29lk"
 }
 }
 
 | 
- 获取指定 ID 的文章数据
 GET /api/getPostById/:id - 获取指定 ID 的文章数据
例子: (与 获取指定路径的文章数据 返回的数据相同)
- 获取分类列表
 GET /api/getCategories/ - 获取分类列表
例子:
| 12
 3
 4
 5
 6
 7
 
 | {"type": "getCategories",
 "data": [
 { "name": "test1", "_id": "cl7ps65yb0000x88d98gzczh3" },
 { "name": "test2", "_id": "cl7ps65yd0003x88d7518aszd" }
 ]
 }
 
 | 
- 获取标签列表
 GET /api/getTags/ - 获取标签列表
例子:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | {"type": "getTags",
 "data": {
 "tags": [
 { "name": "test_tag1", "_id": "cl7ps65yc0001x88d71rr08su" },
 { "name": "test_tag2", "_id": "cl7ps65yc0002x88d2kvegq0o" },
 { "name": "test_tag3", "_id": "cl7ps65yd0004x88db7q32te3" }
 ]
 }
 }
 
 | 
接口文档访问地址
API Interface CN