koa2 web开发疑惑求指点
自己摸索着用koa2做web开发,现在发现前台想要请求数据走不通了,想问问哪里做法不合理?
├── controllers
│ └── page.js
├── models
│ └── pageModel.js
├── public
├── routes
│ └── index.js
├── views
│ └── page.ejs
└── app.js
/controllers/page.js
const config = require('../config')
const frontListNewsCenterModel = require('../models/frontListNewsCenter')
module.exports = async (ctx, next) => {
const { companyNews, industryNews } = await frontListNewsCenterModel(ctx);
console.log( companyNews )
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: companyNews,
industryNews: industryNews,
})
}
/models/pageModel.js
const { getCompanyNewsList, getIndustryInfoList } = require('../config').service
const axios = require('axios')
module.exports = async (ctx, next) => {
const page = ctx.query.page || '1' // page
const size = ctx.query.size || '10' // size
const role = ctx.query.role || 'news' // role
let companyNews = []
let industryNews = []
if (role === 'news') {
companyNews = await axios.get(getCompanyNewsList, {
params: {
page: page,
size: size
}
})
if (companyNews.data.code === '2000') {
companyNews = companyNews.data.obj
}
}
if (role === 'industry') {
industryNews = await axios.get(getIndustryInfoList, {
params: {
page: page,
size: size
}
})
if (industryNews.data.code === '2000') {
industryNews = industryNews.data.obj
}
}
return {
companyNews,
industryNews,
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
后台已经提供接口了,那前端直接请求就可以了,你加了层
koa
是想达到怎么样的需求?是哪里走不通