forked from l20/vue-weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
90 lines (71 loc) · 2.05 KB
/
server.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
var path = require('path');
var http = require('http');
var express = require('express');
var proxyMiddleware = require('http-proxy-middleware');
var app = express();
var proxyTable = {
'/weather_mini': 'http://wthrcdn.etouch.cn',
'/v1': 'https://mainsite-restapi.ele.me',
};
// --------------------路由开始
var routes = express.Router();
var apiRoutes = express.Router();
var cityInfo = require('./static/citydata.json');
var forcast24 = require('./static/forcast.json');
routes.get('/', function(req, res, next) {
req.url = '/index.html';
next();
});
app.use(routes);
// 全国县级以上城市天气代码、名称
apiRoutes.get('/citycode', function(req, res) {
res.json({
errno: 0,
data: cityInfo
});
});
// 24小时预报
apiRoutes.get('/forecast24', function(req, res) {
res.json({
errno: 0,
data: forcast24
});
});
// ip接口
apiRoutes.get('/ip', function(req, response) {
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
http.get('http://ip.taobao.com/service/getIpInfo.php?ip=' + ip, function(res) {
res.on('data', function(d) {
// console.error(d);
response.json({
errno: 0,
data: d.toString('ascii')
});
// process.stdout.write(d);
});
}).on('error', function(e) {
console.error(e);
});
});
app.use('/api', apiRoutes);
// --------------------路由代码结束
//
Object.keys(proxyTable).forEach(function(context) {
var options = proxyTable[context]
if (typeof options === 'string') {
options = { target: options, changeOrigin: true }
}
app.use(proxyMiddleware(options.filter || context, options))
})
// default port where dev server listens for incoming traffic
var port = 8080;
// serve pure static assets
app.use(express.static('./vue-weather'))
console.log('> Starting dev server...')
console.log('> Listening at ' + port + '\n')
var server = app.listen(port)
module.exports = {
close: () => {
server.close()
}
}