最近学习一个vue-cli的项目,需要与后台进行数据交互,这里使用本地json数据来模仿后台数据交互流程。然而发现build文件夹下没有dev-server.js文件了,因为新版本的vue-webpack-template 中已经去掉了dev-server.js,取而代之的是webpack.dev.conf.js文件,所以可以在webpack.dev.conf.js里配置本地访问。

将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');
//引入数据,定义数据变量
var appData = require('../data.json');
var seller = appData.seller;
var goods = appData.goods;
var ratings = appData.ratings; const HOST = process.env.HOST;
const PORT = process.env.PORT && Number(process.env.PORT); 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: {
before(app) {
app.get('/api/seller', (req, res) => {
res.json({
errno: 0,//定义规范,数据是正常的时候用0标识一下,真实的可以不为0的,根据业务方定的
data: seller //用data是更好一点
});
}),
app.get('/api/goods', (req, res) => {
res.json({
errno: 0,//定义规范,数据是正常的时候用0标识一下,真实的可以不为0的,根据业务方定的
data: goods //用data是更好一点
});
}),
app.get('/api/ratings', (req, res) => {
res.json({
errno: 0,//定义规范,数据是正常的时候用0标识一下,真实的可以不为0的,根据业务方定的
data: ratings //用data是更好一点
});
});
},
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,
}
},
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)
}
})
})

 

npm run dev,启动后输入http://localhost:8080/api/seller,如果数据正常显示,则数据能正常返回

PS:Google可以用 jsonview 插件将返回数据格式化

 

mock数据时,http://localhost:8080/#/api/goods 无法访问到数据的更多相关文章

  1. inserted触发器,一张表插入数据时,同时向另外一张表插入数据

    有时候,一个服务器上有多个数据库,需要向其中一个数据库的表中插入数据时, 同时向另外一个数据的表里插入数据. 可以利用触发器和同义词(建立同义词的方法省略), 在一个数据库的表里插入数据时,同时向另外 ...

  2. Node 跨域问题 Access to XMLHttpRequest at 'http://localhost:8080/api/user/login' from origin 'http://localhost:808

    人不可能踏进同一条河流,我可以一天在同一个问题上摔倒两次. 这次是跨域问题,都是泪,教程提供的服务端代码虽然配置了文件,但是依然是没有解决跨域问题,依然报错 Request header field ...

  3. 扩展当easyui datagrid无数据时,显示特定值。如:没有数据

    var myview = $.extend({},$.fn.datagrid.defaults.view,{ onAfterRender:function(target){ $.fn.datagrid ...

  4. Tomcat localhost 8080打不开

    最近发现一个问题,使用tomcat部署项目时,项目可以正常访问,但是localhost 8080打不开,总是出现404 为了找回那只可爱的tom,我先做了个测试 在tomcat的webapps文件夹下 ...

  5. Logstash使用jdbc_input同步Mysql数据时遇到的空时间SQLException问题

    今天在使用Logstash的jdbc_input插件同步Mysql数据时,本来应该能搜索出10条数据,结果在Elasticsearch中只看到了4条,终端中只给出了如下信息 [2017-08-25T1 ...

  6. FreeSql (七)插入数据时忽略列

    var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" + "Initia ...

  7. FreeSql (八)插入数据时指定列

    插入数据时指定列,和忽略列对应,未被指定的列将被忽略. var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Passwor ...

  8. FreeSql (十二)更新数据时指定列

    var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" + "Initia ...

  9. FreeSql (十三)更新数据时忽略列

    var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" + "Initia ...

随机推荐

  1. Pikachu练习平台(暴力破解)

    Pikachu练习平台(暴力破解) 因为下面要用到burp suite,这里先简单介绍一下intruder模块的东西 Target选项:        设置攻击目标,可以通过proxy发送 Pasit ...

  2. 老生常谈--Java值传递和引用传递

    起因 前两天面试被问到了这个问题,虽然之前老早就了解过这个问题,但是并没有深入了解,所以面试的时候一下子慌了,菜是原罪,今天菜鸡来补补基础知识. 其实这个问题一直是被讨论的,常见的三种说法就是,1,J ...

  3. 7_2 最大乘积(UVa11059)<枚举连续子序列>

    给一个数字集合{ S1,S2,…,Sn },请从这个数字集合里找出一段连续数字,使他们的乘积是最大的.以Case 1为例子,2 x 4 = 8为这个集合的最大乘积:而Case 2则为2 x 5 x(– ...

  4. HTML前端入门归纳——布局

    本人一直在从事.net的开发,界面都是采用的WPF,近期花了一个多月进行HTML前端的学习,在这里呢进行学习总结和归纳. 本系列将主要分为4个模块: 控件 样式 布局 JavaScript 根据多年W ...

  5. configure: error: no acceptable C compiler found in $PATH 解决

    在安装keepalived时报错 ./configure --prefix=/usr/local/ccbase/keepalived-2.0.15 && make && ...

  6. hadoop3.1.1 HA高可用分布式集群安装部署

    1.环境介绍 涉及到软件下载地址:https://pan.baidu.com/s/1hpcXUSJe85EsU9ara48MsQ 服务器:CentOS 6.8 其中:2 台 namenode.3 台 ...

  7. Codeforces Round #621 (Div. 1 + Div. 2) D

    题意: 给n,m,k,有n个点,m条线,距离都是一: 有k个特殊点,选择其中两个,进行相连,距离变为1,使得原本的最短路,经过相连改变小或者不变,最终结果是所有结果里面的最大距离. 思路: 选择i,j ...

  8. P &R 12

    Floorplan包含: IO floorplan: 涉及板级设计.封装设计的交互,接口协议(timing相关),对一些高速接口需要做特殊考虑(如信号完整性等). Power plan:芯片的电源和低 ...

  9. Python - 运行流程图, call graph, 调用图

    解决方案 pycallgraph(感觉直接用pycallgraph grahviz命令生成的图并不是我想要的) 如何去阅读并学习一些优秀的开源框架的源码? - mailto1587的回答 - 知乎 h ...

  10. 【Math】高数-一个有趣的旋转体体积与面积

    Go confidently in the direction of your dreams. Live the life you've imagined. 题目 设曲线 \(y = \tfrac{1 ...