Who can tell me why the, koa request server forwards why the front-end access is a 404?

this is the code for forwarding, but the front end of ctx.response.body is 404

.
router.get("/getData", async (ctx, next) => {

  let opts = Object.keys(ctx.query).reduce((pre, item, index) => {
    return pre += `${item}=${encodeURIComponent(ctx.query[item])}&`
  }, "?")
  
  // const getData = await request.get(`https://searchapi.hc360.com/getmmtlast.cgi${opts}`)
  // console.log("getData", getData.query);

  request({
    url: `https://searchapi.hc360.com/getmmtlast.cgi${opts}`,
    method: "GET",
    encoding: null
  }, function(err, res, data) {
    if (!err && res.statusCode == 200) {
      console.log("data", Iconv.decode(data, "gbk"));
      ctx.response.body = Iconv.decode(data, "gbk")
    } else ctx.response.body = err;
  })
  await next();
})
Jan.18,2022

if you want to learn to forward an interface, you can check
if it is to solve the problem of interface cross-domain or cross-service request interface, you can use koa-server-http-proxy

const proxy = require('koa-server-http-proxy');
app.use(proxy('/api', {
  target: 'https://www.shoujinwang.com/',
  pathRewrite: { '^/api': '/' },
  changeOrigin: true
}))

can be forwarded directly to interfaces with custom prefixes


because request is an asynchronous process, ctx.response.body is not executed when requesting / getData , where the result of await request is assigned to ctx

Menu