【发布时间】:2018-05-07 14:51:21
【问题描述】:
我是 koa2 的新手,我正在尝试使用 koa2 获取 JSON 文件的内容
app.use( async ( ctx ) => {
let url = ctx.request.url;
if (url == "list") {
let res = ctx.request.get('http://domain/hello.json');
ctx.body = res.body;
}
})
JSON 文件 hello.json 如下所示:
{"da": "1212", "dad": "12addsf12"}
我希望路由/list 返回hello.json 的内容,但是响应为空。我该怎么办?
更新: 更改以下代码行:
let res = ctx.request.get('http://domain/hello.json');
ctx.body = res.body;
到:
let res = ctx.get('http://domain/hello.json');
ctx.body = res;
你现在应该得到内容了。
【问题讨论】:
-
你已经定义了一个中间件,但是处理
/list路由路径的代码在哪里呢? JSON 文件托管在您的公共目录中还是其他域中? -
ctx.get()不只是ctx.request.get()的别名吗?我现在很好奇:-o