1.下载express

...

2.使用vue-cli下载好项目文件

...

3.找到文件 build - webpack.dev.conf.js

'use strict'
const utils = require('./utils')
const webpack = require('webpack')
const config = require('../config')
const merge = require('webpack-merge')
const path = require('path')
const baseWebpackConfig = require('./webpack.base.conf')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const portfinder = require('portfinder') const HOST = process.env.HOST
const PORT = process.env.PORT && Number(process.env.PORT) /**
* 使用express框架启动一个服务器
*/
var express = require('express')
var app = express()
//1.读取数据
var appData=require('../static/json/table.json');
var businessNum=appData.businessNum;
var recruitingNum=appData.businessNum;
var customerData=appData.businessNum; //2.使用expresss配置路由,指定接口请求
var apiRoutes=express.Router(); //定义一个路由 const devWebpackConfig = merge(baseWebpackConfig, {
module: {
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
},
// cheap-module-eval-source-map is faster for development
devtool: config.dev.devtool, // these devServer options should be customized in /config/index.js
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(apiRoutes) {
apiRoutes.get('/api/businessNum', (req, res) => {
res.json({
// 这里是你的json内容
errno: 0,
data: businessNum
})
});
apiRoutes.get('/api/recruitingNum', (req, res) => {
res.json({
// 这里是你的json内容
errno: 0,
data: recruitingNum
})
});
apiRoutes.get('/api/customerData', (req, res) => {
res.json({
// 这里是你的json内容
errno: 0,
data: customerData
})
})
}
},
plugins: [
new webpack.DefinePlugin({
'process.env': require('../config/dev.env')
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
new webpack.NoEmitOnErrorsPlugin(),
// https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: true
}),
// copy custom static assets
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../static'),
to: config.dev.assetsSubDirectory,
ignore: ['.*']
}
])
]
}) module.exports = new Promise((resolve, reject) => {
portfinder.basePort = process.env.PORT || config.dev.port
portfinder.getPort((err, port) => {
if (err) {
reject(err)
} else {
// publish the new Port, necessary for e2e tests
process.env.PORT = port
// add port to devServer config
devWebpackConfig.devServer.port = port // Add FriendlyErrorsPlugin
devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
compilationSuccessInfo: {
messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`],
},
onErrors: config.dev.notifyOnErrors
? utils.createNotifierCallback()
: undefined
})) resolve(devWebpackConfig)
}
})
})

4.在页面中发送axios请求(先在main.js中引入axios)

...
import axios from 'axios'
...
/* 全局引入axios */
Vue.prototype.$http = axios

5.在页面中请求数据

  created () {
this.$http.get('/api/businessNum').then(response => {
console.log(response)
}).catch(response => {
console.log(response)
})
}

6.页面加载返回数据信息

Vue(二十一)使用express模拟接口数据的更多相关文章

  1. vue项目中使用mockjs模拟接口返回数据

    Mock.js 是一个模拟数据生成器,利用它,可以拦截ajax请求,直接模拟返回数据,这样前后端只要约定好数据格式,前端就不需要依赖后端的接口,可以直接使用模拟的数据了. 网上介绍mock的教程也较多 ...

  2. vue mock 模拟接口数据

    日常总结 希望能帮到大家 1 mock/sever.js  //创建服务 let http=require('http') let fs=require('fs') let url=require(' ...

  3. vue -webpack.dev.config.js模拟后台数据接口

    在const portfinder = require('portfinder')后面添加 const express = require('express') const app = express ...

  4. 用Vue来实现音乐播放器(二十一):歌手详情数据抓取

    第一步:在api文件夹下的singer.js中抛出getSingerDetail方法 第二步:在singer-detail.vue组件中引入api文件夹下的singer.js和config.js 第三 ...

  5. 学习Spring Boot:(二十一)使用 EhCache 实现数据缓存

    前言 当多次查询数据库影响到系统性能的时候,可以考虑使用缓存,来解决数据访问新能的问题. SpringBoot 已经为我们提供了自动配置多个 CacheManager 的实现,只要去实现使用它就可以了 ...

  6. 11. react 基础 使用charles 模拟接口数据

    charles参考文档 charles官网 模拟数据 模拟 axios 请求的数据 eg: 1. 编写 axios 请求 axios.get('/api/xxx') .then(()=>{ale ...

  7. 使用http-server 模拟接口数据 mock data

    首先创建文件夹 mkdir mockData&&cd mockData 安装http-server npm i http-server 创建数据文件 touch index.json ...

  8. 云计算设计模式(二十一)——Sharding分片模式

    云计算设计模式(二十一)——Sharding分片模式 将一个数据存储到一组水平分区或碎片.存储和访问大量数据时,这个模式可以提高可扩展性. 背景和问题 由一个单一的服务器托管的数据存储区可能会受到以下 ...

  9. 深入浅出的webpack4构建工具---比mock模拟数据更简单的方式(二十一)

    如果想要了解mock模拟数据的话,请看这篇文章(https://www.cnblogs.com/tugenhua0707/p/9813122.html) 在实际应用场景中,总感觉mock数据比较麻烦, ...

随机推荐

  1. springboot中使用拦截器、监听器、过滤器

     拦截器.过滤器.监听器在web项目中很常见,这里对springboot中怎么去使用做一个总结. 1. 拦截器(Interceptor)   我们需要对一个类实现HandlerInterceptor接 ...

  2. ASP.NET machineKey的作用和使用方法

    ASP.NET machineKey的作用 如果你的Asp.Net程序执行时碰到这种错误:“验证视图状态 MAC 失败.如果此应用程序由网络场或群集承载,请确保 <machineKey> ...

  3. mysql查看变量/配置文件位置

    //查看所有变量 show global variables //查看某个变量的值 show global variables where variable_name like '%innodb_fl ...

  4. Codeforces 316G3 Good Substrings 字符串 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/9010851.html 题目传送门 - Codeforces 316G3 题意 给定一个母串$s$,问母串$s$有 ...

  5. java项目打包生成MD5文件

    之所以发出这篇博客,因为我前几天搞这个问题搞了几天,各种百度居然都没有找到相关的案例,虽然很简单的事件.可是百度博客上面居然都搜不到案例o(* ̄︶ ̄*)o觉得奇怪!!! 新总监来了,项目要上线,以前都 ...

  6. centos7下docker启动失败解决

    docker启动失败解决 could not change group /var/run/docker.sock to docker: gr... not found 如果出现:Job for doc ...

  7. Editor HDU - 4699 (栈)

    Problem Description   Sample Input 8 I 2 I -1 I 1 Q 3 L D R Q 2   Sample Output 2 3 Hint The followi ...

  8. redis5.0.0.版设置开机自启

  9. [OC] 使用 cocoaPods 导入 AFNetworking

    AFNetworking的GitHub地址: https://github.com/AFNetworking/AFNetworking 假设我们建立了一个叫做AFNWlearning的工程. 1.打开 ...

  10. 2、SQL UNION 和 UNION ALL 操作符

    网址:http://www.w3school.com.cn/sql/sql_union.asp SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意, ...