鍍金池/ 問答/Linux  HTML/ koa2怎么判斷一個接口返回的是HTML視圖還是文本數(shù)據(jù)

koa2怎么判斷一個接口返回的是HTML視圖還是文本數(shù)據(jù)

接口如下

router.get('/a', async (ctx, next) => {
  await ctx.render('index', {})
})

router.get('/b', (ctx, next) => {
  ctx.body = 'b'
})

router.get('/c', (ctx, next) => {
  ctx.body = { c: 'c' }
})

一個中間件如下,怎么判斷await next返回的是不是視圖,如果是視圖,則不進行結(jié)果包裝。

app.use(async (ctx, next) => {
  let success = true
  let message = ''
  try {
    await next()
  } catch (e) {
    success = false
    message = e.message
  }
  ctx.body = {
    success,
    message,
    data: ctx.body
  }
})
回答
編輯回答
獨白

檢查下字符串是否包含特定標(biāo)簽字段如<html>

2017年3月30日 00:24