koa-router 使用问题!
如何在/routes/news.js
的router.get('/'
里面获取/controllers/frontListNewsCenter.js
数据
/routes/news.js
const router = require('koa-router')({
prefix: '/news'
})
const config = require('../config')
const controllers = require('../controllers')
router.get('/', async (ctx, next) => {
const data = await controllers.frontListNewsCenter.get(ctx, next)
console.log(111, data) // undefined
// 在这里怎么获取 /controllers/frontListNewsCenter.js 数据?
await ctx.render('frontListNewsCenter', {
header: {
title: '新闻中心',
javascript: config.javascript.concat('/js/jqpaginator.min.js', '/js/frontListNewsCenter.js'),
style: config.style.concat('/css/frontListNewsCenter.css'),
},
newBanner: {
imgSrc: '/images/newsbannenr.png',
},
tabNav: {
items: [{
cn: '公司新闻',
en: 'Company news',
}, {
cn: '行业咨询',
en: 'Industry information',
}],
},
companyNews: [],
industryNews: [],
})
})
router.get('/getList', controllers.frontListNewsCenter.get)
module.exports = router
/controllers/frontListNewsCenter.js
const { getCompanyNewsList, getIndustryInfoList } = require('../config').service
const axios = require('axios')
async function get (ctx, next) {
const page = ctx.query.page || '1' // page
const size = ctx.query.size || '10' // size
const role = ctx.query.role || 'news' // role
console.log(222, page, size, role)
if (role === 'news') {
const companyNews = await axios.get(getCompanyNewsList, {
params: {
page: page,
size: size
}
})
if (companyNews.data.code === '2000') {
ctx.state.code = 0
ctx.state.data = companyNews.data.obj
} else {
ctx.state.code = -1
ctx.state.data = []
}
}
if (role === 'industry') {
const industryNews = await axios.get(getIndustryInfoList, {
params: {
page: page,
size: size
}
})
if (industryNews.data.code === '2000') {
ctx.state.code = 0
ctx.state.data = industryNews.data.obj
} else {
ctx.state.code = -1
ctx.state.data = []
}
}
}
module.exports = {
get
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样试试
你要获取函数里面的数据,只能通过调用呀