vue2.5.2通过http请求获取静态的json数据
2019独角兽企业重金招聘Python工程师标准>>>
文章图片
1.配置 build/webpack.dev.conf.js
// 获取静态json数据
const express = require('express')
const app = express()
const apiServer = express()
const bodyParser = require('body-parser')
apiServer.use(bodyParser.urlencoded({ extended: true }))
apiServer.use(bodyParser.json())
const apiRouter = express.Router()
const fs = require('fs')
apiRouter.route('/:apiName')
.all(function (req, res) {
fs.readFile('./db.json', 'utf8', function (err, data) {
if (err) throw err
var data = https://www.it610.com/article/JSON.parse(data)
if (data[req.params.apiName]) {
res.json(data[req.params.apiName])
}
else {
res.send('no such api name')
}})
})apiServer.use('/api', apiRouter);
apiServer.listen(8081, function (err) {
if (err) {
console.log(err)
return
}
console.log('Listening at http://localhost:' + (8081) + '\n')
})
【vue2.5.2通过http请求获取静态的json数据】2.新建 db.json
{
"getNewsList": [
{
"id": 1,
"title": "新闻条目1新闻条目1新闻条目1新闻条目1",
"url": "http://starcraft.com"
},
{
"id": 2,
"title": "新闻条目2新闻条目2新闻条目2新闻条目2",
"url": "http://warcraft.com"
},
{
"id": 3,
"title": "新闻条3新闻条3新闻条3",
"url": "http://overwatch.com"
},
{
"id": 4,
"title": "新闻条4广告发布",
"url": "http://hearstone.com"
}
],
"login": {
"username": "yudongdong",
"userId": 123123
},
"getPrice": {
"amount": 678
},
"createOrder": {
"orderId": "6djk979"
},
"getOrderList": {
"list": [
{
"orderId": "ddj123",
"product": "数据统计",
"version": "高级版",
"period": "1年",
"buyNum": 2,
"date": "2016-10-10",
"amount": "500元"
},
{
"orderId": "yuj583",
"product": "流量分析",
"version": "户外版",
"period": "3个月",
"buyNum": 1,
"date": "2016-5-2",
"amount": "2200元"
},
{
"orderId": "pmd201",
"product": "广告发布",
"version": "商铺版",
"period": "3年",
"buyNum": 12,
"date": "2016-8-3",
"amount": "7890元"
}
]
}
}
3.通过 localhost:8081/api/getNewsList 访问
4.在页面中获取的方式
export default {
data() {
newsList: []
},
created: function(){
this.$http.get('api/getNewsList').then((res)=> {
this.newsList = res.data
},(err)=> {
console.log(err);
})
}
}
转载于:https://my.oschina.net/u/2489258/blog/1624464
推荐阅读
- 画解算法(1.|画解算法:1. 两数之和)
- gitlab|gitlab 通过备份还原 admin/runner 500 Internal Server Error
- ts泛型使用举例
- https请求被提早撤回
- HTTP高级(Cookie,Session|HTTP高级(Cookie,Session ,LocalStorage )
- 拒绝可以很艺术,或者很行为艺术。
- whlie循环和for循环的应用
- www和https://又被Chrome地址栏隐藏了
- Spring集成|Spring集成 Mina
- 如何通过锻炼的方法治疗前列腺肥大