hexo-跨域方法

hexo 跨域方法

案例demo

1.安装http-proxy-middleware:

1
npm install --save-dev http-proxy-middleware

2.新增文件 themes/landscape/scripts/http_proxy_middleware.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const { createProxyMiddleware } = require('http-proxy-middleware');

const apiProxy = createProxyMiddleware('/api', {
target: 'http://baidu.com',
changeOrigin: true,
pathRewrite: {
'^/api/': '/', // rewrite path 将链接中的 /api/ 替换为 '/'
},
});

hexo.extend.filter.register('server_middleware', function(app){
// 表示以 api 开头的请求将被转发
app.use('/api', apiProxy);
});

3.重启项目即可hexo server,出现以下信息表示配置成功:

1
2
[HPM] Proxy created: /api  -> http://baidu.com
[HPM] Proxy rewrite rule created: "^/api/" ~> "/"