我在使用Koa和SuperAgent写爬虫脚本时遇到的end()报错
发布于 3 天前 作者 CNMathon 386 次浏览 来自 问答

我在使用Koa和SuperAgent写爬虫脚本时遇到了些问题。 但是当我运行项目时,会引发报错。

代码

const Koa = require('koa');
// const request = require('request')
const superagent= require('superagent');

const app = new Koa();


app.use(async ctx => {
  const res = await superagent.get('https://www.google.com');
  ctx.body = res
})


app.listen(3000);

报错信息

Error: end() has already been called, so it's too late to start piping
      at Response.response.pipe (F:\Project\Self_Project\Boxcc\node_modules\superagent\src\node\index.js:923:13)
      at respond (F:\Project\Self_Project\Boxcc\node_modules\koa\lib\application.js:256:43)
      at handleResponse (F:\Project\Self_Project\Boxcc\node_modules\koa\lib\application.js:164:34)
      at process._tickCallback (internal/process/next_tick.js:68:7)

(node:20500) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

  Error: end() has already been called, so it's too late to start piping
      at Response.response.pipe (F:\Project\Self_Project\Boxcc\node_modules\superagent\src\node\index.js:923:13)
      at respond (F:\Project\Self_Project\Boxcc\node_modules\koa\lib\application.js:256:43)
      at handleResponse (F:\Project\Self_Project\Boxcc\node_modules\koa\lib\application.js:164:34)
      at process._tickCallback (internal/process/next_tick.js:68:7)
1 回复

中间件要调用await next()

https://github.com/koajs/koa/blob/master/docs/guide.md

如果还不清楚可以开个debug

DEBUG=koa* node --harmony app.js

回到顶部