在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML/ 在vue-cli中mock的數(shù)據(jù)訪問(wèn)不到

在vue-cli中mock的數(shù)據(jù)訪問(wèn)不到

在vue腳手架工具基礎(chǔ)上添加了一個(gè)data.json文件
在dev-server.js中添加如下配置,然后重新npm run dev。訪問(wèn)http://localhost:8080/api/seller訪問(wèn)不到數(shù)據(jù)
clipboard.png

// mock
const app = express()
const appData = require('../data.json')
const seller = appData.seller
const goods = appData.goods
const ratings = appData.ratings
const apiRoutes = express.Router();
apiRoutes.get('./seller', function(req, res) {
  res.json({
    erron: 0,
    data: seller
  })
})
apiRoutes.get('./ratings', function(req, res) {
  res.json({
    erron: 0,
    data: ratings
  })
})
apiRoutes.get('./goods', function(req, res) {
  res.json({
    erron: 0,
    data: goods
  })
})
app.use('./api', apiRoutes)
回答
編輯回答
胭脂淚

改幾個(gè)地方,把"./*"改成"/";

const app = express()
const appData = require('../data.json')
const seller = appData.seller
const goods = appData.goods
const ratings = appData.ratings
const apiRoutes = express.Router();
apiRoutes.get('/seller', function(req, res) {
  res.json({
    erron: 0,
    data: seller
  })
})
apiRoutes.get('/ratings', function(req, res) {
  res.json({
    erron: 0,
    data: ratings
  })
})
apiRoutes.get('/goods', function(req, res) {
  res.json({
    erron: 0,
    data: goods
  })
})
app.use('/api', apiRoutes)
2018年3月2日 14:29
編輯回答
孤慣
var app = express()
// 1. 加載json數(shù)據(jù)
const data = require('../src/mock/data.json')
// 2. 生成路由器
const router = express.Router()
// 3. 注冊(cè)路由
router.get('/goods', function (req, res, next) { // 處理請(qǐng)求, 返回響應(yīng)數(shù)據(jù)
  res.send({ // 返回給瀏覽器的是包含數(shù)據(jù)的對(duì)象
    code: 0,  // 數(shù)據(jù)的標(biāo)識(shí)屬性   0代表正確的數(shù)據(jù)
    data: data.goods
  })
})
router.get('/ratings', function (req, res, next) { // 處理請(qǐng)求, 返回響應(yīng)數(shù)據(jù)
  res.send({
    code: 0,
    data: data.ratings
  })
})
router.get('/seller', function (req, res, next) { // 處理請(qǐng)求, 返回響應(yīng)數(shù)據(jù)
  res.send({
    code: 0,
    data: data.seller
  })
})
// 4. 啟用路由器
app.use('/api', router)

你把'./'換成'/'看看

2017年10月15日 03:35