forked from pusongyang/todolist-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-faas.js
142 lines (137 loc) · 3.58 KB
/
index-faas.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
// 阿里云FaaS部署,主进程代码
const { Server } = require('@webserverless/fc-express')
const express = require('express');
const fs = require('fs');
const path = require('path');
const bodyParser = require('body-parser');
// initial todo list
let todos = [
{
key: 1,
disabled: false,
href: 'https://ant.design',
avatar: 'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',//'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
name: `来自后端的你`,
owner: '秦粤',
desc: '这是一段描述',
callNo: 18890992445,
status: 1,
updatedAt: new Date(),
createdAt: new Date(),
progress: 0,
},
];
const app = express();
// 静态资源路由
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// 后端服务路由
app.get('/api/rule', (req, resp) => {
const { current = 1, pageSize = 10 } = req.query;
const result = {
data: todos,
total: todos.length,
success: true,
pageSize,
current: current || 1,
};
resp.json(result);
});
app.post('/api/rule', (req, res) => {
const body = req.body;
const { method, name, desc, key, status } = body;
switch (method) {
case 'delete':
todos = todos.filter(item => key.indexOf(item.key) === -1);
break;
case 'post':
(() => {
const i = todos.length+1;
const newRule = {
key: i,
href: 'https://ant.design',
avatar: [
'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
][i % 2],
name,
owner: '曲丽丽',
desc,
callNo: Math.floor(Math.random() * 1000),
status: 1,
updatedAt: new Date(),
createdAt: new Date(),
progress: 0,
};
todos.unshift(newRule);
return res.json(newRule);
})();
return;
case 'update':
(() => {
let newRule = {};
todos = todos.map(item => {
if (item.key === key) {
newRule = { ...item, desc, name, status };
return { ...item, desc, name, status };
}
return item;
});
return res.json(newRule);
})();
return;
default:
break;
}
const result = {
list: todos,
pagination: {
total: todos.length,
},
};
res.json(result);
})
app.get('/api/currentUser', (req, resp) => {
const me = {
name: 'Serati Ma',
avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
userid: '00000001',
email: '[email protected]',
signature: '陕西的秦,广东的粤',
title: '全栈工程师',
group: '某厂-某事业群-某平台部-某技术部-中台团队',
tags: [
{
key: '0',
label: '老司机',
},
],
notifyCount: 12,
unreadCount: 11,
country: 'China',
geographic: {
province: {
label: '浙江省',
key: '330000',
},
city: {
label: '杭州市',
key: '330100',
},
},
address: '余杭区某小区',
phone: '0752-26888xxxx',
};
resp.json(me);
});
// SPA单页应用,默认加载index.html
app.all("/*", (req, resp) => {
resp.setHeader('Content-Type', 'text/html');
resp.send(fs.readFileSync('./public/index.html', 'utf8'));
});
阿里云FaaS部署
const server = new Server(app);
module.exports.handler = function(req, res, context) {
server.httpProxy(req, res, context);
};