zoukankan      html  css  js  c++  java
  • 高仿饿了么mock本地数据

    未使用router
    新版webpack.dev.conf.js配置本地数据访问:
    // 引入express 模块 const express = require('express') // 创建express对象 const app = express() // 引入请求文件 加载本地数据文件 const appData = require('../data.json') // 获取对应的本地数据 const seller = appData.seller const goods = appData.goods const ratings = appData.ratings

    devServer: {
        clientLogLevel: 'warning',
        historyApiFallback: {
          rewrites: [
            { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
          ],
        },
        
        hot: true,
        contentBase: false, // since we use CopyWebpackPlugin.
        compress: true,
        host: HOST || config.dev.host,
        port: PORT || config.dev.port,
        open: config.dev.autoOpenBrowser,
        overlay: config.dev.errorOverlay
          ? { warnings: false, errors: true }
          : false,
        publicPath: config.dev.assetsPublicPath,
        proxy: config.dev.proxyTable,
        quiet: true, // necessary for FriendlyErrorsPlugin
        watchOptions: {
          poll: config.dev.poll,
        },
        before (app) {
          // 创建不同的路由对象  配置接口
          app.get('/api/seller', (req, res) => {
            res.json({
              errno: 0,
              data: seller //接口返回json数据,上面配置的数据seller就赋值给data请求后调用
           })
          })
          app.get('/api/goods', (req, res) => {
            res.json({
              errno: 0,
              data: goods
            })
          })
          app.get('/api/ratings', (req, res) => {
            res.json({
              errno: 0,
              data: ratings
            })
          })
        }
      },

    在devServer中before方法中配置接口

    使用router:

    新版webpack.dev.conf.js配置本地数据访问:
    // 引入express 模块
    const express = require('express')
    // 创建express对象
    const app = express()
    定义router
    const apiRoutes = express.Router()
    // 引入请求文件 加载本地数据文件 const appData = require('../data.json') // 获取对应的本地数据 const seller = appData.seller const goods = appData.goods const ratings = appData.ratings

    apiRoutes.get('getOrderList', (res) =>{
    res.json({
    errno: 0
    data: getOrderList
    //返回值为json格式,效验码编程时方便判断返回状态

    })
    })
    app.use(apiRoutes)
  • 相关阅读:
    javascript获取url地址问好后面的值,包括问号
    给Object扩展新方法
    关于hasOwnProperty()方法的应用
    javascript if 与 if else 的一点小认识
    express中路由设置的坑-----1
    javascript数组顺序-----1冒泡的另一种比较好理解的写法
    进程的控制与状态细化
    进程的定义与控制
    进程的定义与控制
    进程引入
  • 原文地址:https://www.cnblogs.com/qwert1/p/8761352.html
Copyright © 2011-2022 走看看