为什么以下代码在webbroswer中返回xml输出?

const Koa = require('koa');
const app = new Koa();

const main = ctx => {
  if (ctx.request.accepts('xml')) {
    ctx.response.type = 'xml';
    ctx.response.body = '<data>Hello World</data>';
  } else if (ctx.request.accepts('json')) {
    ctx.response.type = 'json';
    ctx.response.body = { data: 'Hello World' };
  } else if (ctx.request.accepts('html')) {
    ctx.response.type = 'html';
    ctx.response.body = '<p>Hello World</p>';
  } else {
    ctx.response.type = 'text';
    ctx.response.body = 'Hello World';
  }
};

app.use(main);
app.listen(3000);

我正在为nodejs http://www.ruanyifeng.com/blog/2017/08/koa.html播放此koa网络框架。但是,我很困惑。我从逻辑上不理解为什么上面的代码运行后会返回xml输出。谁能遮住一些灯?

yangjuan1987 回答:为什么以下代码在webbroswer中返回xml输出?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3166016.html

大家都在问