-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_hot.js
More file actions
135 lines (119 loc) · 3.48 KB
/
server_hot.js
File metadata and controls
135 lines (119 loc) · 3.48 KB
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
var webpack = require('webpack');
var express = require('express');
var config = require('./webpack.config.hot');
var proxyMiddleware = require('http-proxy-middleware')
//对外请求
const request=require('request');
var app = express();
var compiler = webpack(config);
//热重载
app.use(require('webpack-dev-middleware')(compiler, {
publicPath: config.output.publicPath,
hot: true,
historyApiFallback: true,
inline: true,
progress: true,
stats: {
colors: true,
}
}));
//代理服务器
app.use('/shopro', proxyMiddleware({
target: 'http://dev.fe.ptdev.cn',
changeOrigin: true,
}))
app.use(require('webpack-hot-middleware')(compiler));
// middleware config start
app.use((req,res,next)=>{
// 增加了cors跨域的请求头--这里好像不用也是可以的
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
next();
})
//好像确实是可以当中间件啊
app.get("/getMovieList", (req, res, next)=>{
//从 req.query中获取就可以了
console.log(req.query);
let url = `https://api.douban.com/v2/movie/${req.query.type}?start=${req.query.start}&count=${req.query.count}&city=${req.query.city}`;
console.log(url);
// let url = "https://api.douban.com/v2/movie/in_theaters?start=10&count=15&city=深圳";
// https://api.douban.com/v2/movie/in_theaters?start=10&count=15&city=深圳
//发起请求是request
/*request(url, (error,response, body)=>{
// console.log(body);
res.send(body)
})*/
res.send({
"count": 15,
"start": 10,
"total": 25,
"subjects": [
{
"rating": {
"max": 10,
"average": 6.8,
"stars": "35",
"min": 0
},
"genres": [
"剧情",
"动作",
"犯罪"
],
"title": "缉枪",
"casts": [
{
"alt": "https://movie.douban.com/celebrity/1340456/",
"avatars": {},
"name": "白举纲",
"id": "1340456"
},
{
"alt": "https://movie.douban.com/celebrity/1312979/",
"avatars": {
"small": "https://img3.doubanio.com/img/celebrity/small/14640.jpg",
"large": "https://img3.doubanio.com/img/celebrity/large/14640.jpg",
"medium": "https://img3.doubanio.com/img/celebrity/medium/14640.jpg"
},
"name": "连奕名",
"id": "1312979"
}
],
"collect_count": 795,
"original_title": "缉枪",
"subtype": "movie",
"directors": [
],
"year": "2017",
"images": {
"small": "https://img3.doubanio.com/view/movie_poster_cover/ipst/public/p2461747793.webp",
"large": "https://img3.doubanio.com/view/movie_poster_cover/lpst/public/p2461747793.webp",
"medium": "https://img3.doubanio.com/view/movie_poster_cover/spst/public/p2461747793.webp"
},
"alt": "https://movie.douban.com/subject/26801782/",
"id": "26801782"
}
]
});
// middleware config end
});
app.get('/getCity.json', (req, res, next)=>{
let timer = setTimeout(()=>{
res.sendFile(__dirname + '/src/data/getCity.json');
clearTimeout(timer);
}, 2000);
})
app.get('/getFunc.json', (req, res, next)=>{
let timer = setTimeout(()=>{
res.sendFile(__dirname + '/src/data/getFunc.json')
clearTimeout(timer);
}, 2000);
})
//将其他路由,全部返回index.html
app.get('*', function(req, res) {
res.sendFile(__dirname + '/index.html')
});
app.listen(8088, function() {
console.log('正常打开8088端口 hot......')
});