Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 81119e4

Browse files
committed
update server.js
1 parent d414b6e commit 81119e4

File tree

1 file changed

+8
-25
lines changed

1 file changed

+8
-25
lines changed

‎server.js‎

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
11
/** 用于开发环境的服务启动 **/
22
const path = require("path"); // 获取绝对路径有用
33
const express = require("express"); // express服务器端框架
4-
const bodyParser = require("body-parser");
54
const env = process.env.NODE_ENV; // 模式(dev开发环境,production生产环境)
65
const webpack = require("webpack"); // webpack核心
76
const webpackDevMiddleware = require("webpack-dev-middleware"); // webpack服务器
87
const webpackHotMiddleware = require("webpack-hot-middleware"); // HMR热更新中间件
98
const webpackConfig = require("./webpack.dev.config.js"); // webpack开发环境的配置文件
10-
const mock = require("./mock/app-data"); // mock模拟数据,模拟后台业务
119

12-
// const { createProxyMiddleware } = require("http-proxy-middleware"); // 跨域配置 需要跨域请打开注释即可
10+
const mock= require("./mock/app-data"); // mock模拟数据,模拟后台业务
1311

1412
const app = express(); // 实例化express服务
13+
const DIST_DIR = webpackConfig.output.path; // webpack配置中设置的文件输出路径,所有文件存放在内存中
1514
let PORT = 8888; // 服务启动端口号
1615

17-
// 跨域设置 需要跨域请打开注释,自己设置对应的域名
18-
// app.use(
19-
// "/proxy",
20-
// createProxyMiddleware({
21-
// target: "https://example.com", // 目标域名
22-
// changeOrigin: true,
23-
// ws: false,
24-
// pathRewrite: {
25-
// "^/proxy": "/",
26-
// },
27-
// })
28-
// );
29-
30-
// bodyParser的配置需要放在Proxy代理的下面,否则post请求的代理参数无法处理
31-
app.use(bodyParser.urlencoded({ extended: false }));
32-
app.use(bodyParser.json());
16+
app.use(express.urlencoded({ extended: false }));
17+
app.use(express.json());
3318

3419
/** 监听POST请求,返回MOCK模拟数据 **/
3520
app.post(/\/api.*/, (req, res, next) => {
@@ -45,24 +30,22 @@ if (env === "production") {
4530
// 如果是生产环境,则运行build文件夹中的代码
4631
PORT = 8889;
4732
app.use(express.static("build"));
48-
app.get("*", function(req, res) {
33+
app.get("*", (req, res)=> {
4934
res.sendFile(path.join(__dirname, "build", "index.html"));
5035
});
5136
} else {
5237
const compiler = webpack(webpackConfig); // 实例化webpack
38+
app.use(express.static("dll"));
5339
app.use(
54-
// 挂载webpack小型服务器
5540
webpackDevMiddleware(compiler, {
5641
publicPath: webpackConfig.output.publicPath, // 对应webpack配置中的publicPath
5742
}),
5843
);
59-
// 挂载HMR热更新中间件
60-
app.use(webpackHotMiddleware(compiler));
6144

45+
app.use(webpackHotMiddleware(compiler)); // 挂载HMR热更新中间件
6246
// 所有请求都返回index.html
6347
app.get("*", (req, res, next) => {
64-
// webpack配置中设置的文件输出路径,所有文件存放在内存中
65-
const filename = path.join(webpackConfig.output.path, "index.html");
48+
const filename = path.join(DIST_DIR, "index.html");
6649

6750
// 由于index.html是由html-webpack-plugin生成到内存中的,所以使用下面的方式获取
6851
compiler.outputFileSystem.readFile(filename, (err, result) => {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /