-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
65 lines (58 loc) · 1.5 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
var express = require('express')
var jade = require('jade')
var app = express()
var low = require('lowdb')
var db = low('./db.json')
var dw = low('./dw.json')
var _ = require('lodash')
app.engine('views', jade.__express)
app.set('view engine', 'jade')
app.set('views', './views/')
app.use(express.static('./static'))
app.get('/', function(req, res){
res.redirect('dw')
})
app.get('/db', function(req, res){
var data = db.object
res.render('db', data)
})
app.get('/dw', function(req, res){
var data = _.cloneDeep(dw.object)
_.each(data.market_fact, function(item){
item.customer = _.find(data.customers, {
id: item.customer_id
})
item.region = _.find(data.regions, {
id: item.region_id
})
item.product = _.find(data.products, {
id: item.product_id
})
item.order_date = _.find(data.dates, {
id: item.order_date_id
})
item.consign_date = _.find(data.dates, {
id: item.consign_date_id
})
})
var q = req.query
if (q.type === 'slice') {
var vals = q.slice_vals.split(/, */)
data.market_fact = _.filter(data.market_fact, function(item){
return _.any(vals, function(v){
return get_val(item, q.slice_key) == v // 类型转换比较
})
})
}
data._type = q.type || ''
data._slice_key = q.slice_key || ''
data._slice_vals = q.slice_vals || ''
res.render('dw', data)
})
app.listen(8115)
function get_val(o, key){
var segs = key.split('.')
return _.reduce(segs, function(p, seg){
return p = p[seg]
}, o)
}