一直不清楚做项目的时候前台怎么和后台进行通信,虽然知道可以通过ajax请求后台数据,但是不知道在具体项目中怎么运用的,正好自己搭项目的时候也遇到了这个问题,简单记录一下。
起因是webpack可以自己指定port(本地代码,host为http://localhost:port),并将文件打包,运行在指定的host上面。同时,后台也指定了程序运行的端口号。若前台指定的端口号和后台一样,肯定会有错误,若指定不同端口号,请求的url又跨域了,此时的做法是在webpack配置上加上一个devserver配置,加上一个代理
1 | devServer: { |
配置 | 说明 |
---|---|
proxy | 请求到 /api/v1 会被代理请求到http://localhost:8025/api/v1 |
contentBase | 告诉服务器从哪个目录内容提供文件 |
compress | 一切服务都启用gzip压缩 |
historyApiFallback | 默认禁用404响应被替代为index.html |
hot | 启用webpack的模块热加载功能(必须有 webpack.HotModuleReplacementPlugin 才能完全启用 HMR) |
port | 指定要监听请求的端口号 |
详细配置见 devserver
webpack.config.js文件:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162const path = require('path');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlwebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackIncludeAssetsPlugin = require('html-webpack-include-assets-plugin');
const OpenBrowerPlugin = require('open-browser-webpack-plugin');
const port = 8026;
const host = `http://localhost:${port}/`;
module.exports = {
entry: {
bundle: './src/index.js',
vendor: [
'classnames',
'redux',
'redux-thunk',
'redux-immutable',
'redux-actions',
'react-router',
'react-router-dom',
'history/createBrowserHistory'
]
},
output: {
path: path.resolve(__dirname, "dist"),
filename: 'js/[name].js',
chunkFilename: 'lib/[name].min.js',
publicPath: host
},
resolve: {
modules: [
"node_modules",
path.resolve(__dirname, "src")
],
extensions: [".js", ".jsx", ".json", ".css", ".scss"]
},
mode: 'development',
devtool: 'eval-source-map',
devServer: {
contentBase: './src',
historyApiFallback: true,
inline: true,
open: true,
hot: true,
compress: true,
port: port
},
module: {
rules: [{
test: /\.jsx?$/,
use: {
loader: 'babel-loader',
query: {
presets: ['react', 'es2017'],
plugins: ['transform-object-rest-spread', "syntax-dynamic-import"]
}
},
exclude: /node_modules/
},{
test: /\.scss$/,
use: ['style-loader', 'css-loader?modules', {
loader: 'postcss-loader',
options: {
plugins: () => [
require('autoprefixer')({ browsers: ['last 10 Chrome versions', 'last 5 Firefox versions', 'Safari >= 6', 'ie > 8'] })
]
}
}, 'sass-loader']
},{
test: /\.less$/,
use: ['style-loader', 'css-loader', {
loader: 'less-loader',
options: {
modifyVars: {
"primary-color": "#169BD5",
"link-color": "#169BD5"
},
javascriptEnabled: true
}
}]
},{
test: /\.css$/,
use: ['style-loader', 'css-loader']
},{
test: /\.(?:png|jpg|gif)/,
loader:'url-loader?limit=8192&name=image/[hash].[ext]'
},{
test: /\.svg$/,
loader: 'svg-url-loader'
}]
},
devServer: {
proxy: {
'/api/v1': 'http://localhost:8025',
},
contentBase: [path.join(__dirname, "dist")],
compress: true,
historyApiFallback: true,
hot: true,
port: port
},
plugins: [
// new webpack.optimize.CommonsChunkPlugin({
// names: ['vendor'],
// filename: 'lib/[name].min.js',
// }),
new webpack.optimize.SplitChunksPlugin({
cacheGroups: {
commons: {
chunks: 'initial',
minChunks: 2,
maxInitialRequetst: 5,
minSize: 0
},
vendor: {
test: /node_modules/,
chunks: 'initial',
name: 'vendor',
priority: 10,
enforce: true
}
}
}),
new CopyWebpackPlugin([{
from: 'node_modules/antd/dist/antd.min.js',
to: '/lib'
},{
from: 'node_modules/react/cjs/react.development.js',
to: '/lib'
},{
from: 'node_modules/react-dom/cjs/react-dom.development.js',
to: '/lib'
}]),
new HtmlwebpackPlugin({
filename: 'index.html',
template: 'src/index.ejs',
title: 'RShare'
}),
new HtmlWebpackIncludeAssetsPlugin({
assets: [
'lib/react-dom.development.js',
'lib/react.development.js',
'lib/antd.min.js'
],
append: false
}),
new OpenBrowerPlugin({
url: host
}),
]
}