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

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

在vue腳手架工具基礎(chǔ)上添加了一個data.json文件
在dev-server.js中添加如下配置,然后重新npm run dev。訪問http://localhost:8080/api/seller訪問不到數(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)
回答
編輯回答
胭脂淚

改幾個地方,把"./*"改成"/";

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. 注冊路由
router.get('/goods', function (req, res, next) { // 處理請求, 返回響應(yīng)數(shù)據(jù)
  res.send({ // 返回給瀏覽器的是包含數(shù)據(jù)的對象
    code: 0,  // 數(shù)據(jù)的標識屬性   0代表正確的數(shù)據(jù)
    data: data.goods
  })
})
router.get('/ratings', function (req, res, next) { // 處理請求, 返回響應(yīng)數(shù)據(jù)
  res.send({
    code: 0,
    data: data.ratings
  })
})
router.get('/seller', function (req, res, next) { // 處理請求, 返回響應(yīng)數(shù)據(jù)
  res.send({
    code: 0,
    data: data.seller
  })
})
// 4. 啟用路由器
app.use('/api', router)

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

2017年10月15日 03:35