forked from shigebeyond/React-Reflux-Ant.Design-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
158 lines (142 loc) · 3.38 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
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
require('babel-register')
const webpack = require('webpack');
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const config = require('./webpack.config');
const isProduction = process.env.NODE_ENV === 'production';
const isDeveloping = !isProduction;
const app = express();
// Webpack developer
if (isDeveloping) {
const compiler = webpack(config);
app.use(require('webpack-dev-middleware')(compiler, {
publicPath: config.output.publicPath,
noInfo: true
}));
app.use(require('webpack-hot-middleware')(compiler));
}
// RESTful API
const publicPath = path.resolve(__dirname);
app.use(bodyParser.json({ type: 'application/json' }))
app.use(express.static(publicPath));
const port = isProduction ? (process.env.PORT || 80) : 3001;
// this is necessary to handle URL correctly since client uses Browser History
app.get('*', function (req, res){
res.sendFile(path.resolve(__dirname, '', 'index.html'))
})
app.put('/api/login', function(req, res) {
const credentials = req.body;
if(credentials.user==='admin' && credentials.password==='123456'){
res.cookie('uid', '1', {domain:'127.0.0.1'});
res.json({'user': credentials.user, 'role': 'ADMIN', 'uid': 1});
}else{
res.status('500').send({'message' : 'Invalid user/password'});
}
});
app.post('/api/menu', function(req, res) {
res.json({
menus: [
{
key: 1,
name: 'Dashboard',
icon: 'user',
child: [
{
name: '选项1',
key: 101
},
{
name: '选项2',
key: 102
},
{
name: '选项3',
key: 103
},
{
name: '选项4',
key: 104
}
]
},
{
key: 2,
name: '导航二',
icon: 'laptop',
child: [
{
name: '选项5',
key: 201
},
{
name: '选项2',
key: 202
},
{
name: '选项3',
key: 203
},
{
name: '选项4',
key: 204
}
]
},
{
key: 3,
name: '导航三',
icon: 'notification',
child: [
{
name: '选项1',
key: 301
},
{
name: '选项2',
key: 302
},
{
name: '选项3',
key: 303
},
{
name: '选项4',
key: 304
}
]
}
]
});
});
app.post('/api/register', function(req, res) {
res.json(req.body);
});
app.post('/api/my', function(req, res) {
res.json({'user': 'admin', 'role': 'ADMIN', 'uid': 1});
});
app.post('/api/logout', function(req, res) {
res.clearCookie('uid');
res.json({'user': 'admin', 'role': 'ADMIN'});
});
// 获得分页数据
app.post('/api/employee', function(req, res) {
const page = req.query.page,
items = [],
total = 20;
for (var i = (page - 1) * 10; i < page * 10; i++) {
items.push({
key: i,
name: `李大嘴${i}`,
birthday: '1986-12-21',
address: `西湖区湖底公园${i}号`,
});
}
res.json({items, total});
});
app.listen(port, function (err, result) {
if(err){
console.log(err);
}
console.log('Server running on port ' + port);
});