-
Notifications
You must be signed in to change notification settings - Fork 17
/
server.js
154 lines (132 loc) · 3.51 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
const http = require('http');
const fs = require('fs');
const Koa = require('koa');
const Router = require('koa-router');
const koaBody = require('koa-body');
const router = new Router();
const app = new Koa();
app.use(
koaBody({
urlencoded: true,
multipart: true,
json: true,
text: true,
formLimit: '10mb',
jsonLimit: '10mb',
textLimit: '10mb',
enableTypes: ['json', 'form', 'text'],
}),
);
app.use(async (ctx, next) => {
const origin = ctx.request.get('Origin');
if (!origin) {
const n = await next();
return n;
}
const headers = { 'Access-Control-Allow-Origin': '*' };
if (ctx.request.method !== 'OPTIONS') {
ctx.response.set({ ...headers });
try {
return await next();
} catch (e) {
e.headers = { ...e.headers, ...headers };
throw e;
}
}
if (ctx.request.get('Access-Control-Request-Method')) {
ctx.response.set({
...headers,
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH',
});
if (ctx.request.get('Access-Control-Request-Headers')) {
ctx.response.set(
'Access-Control-Allow-Headers',
ctx.request.get('Access-Control-Request-Headers'),
);
}
ctx.response.status = 204;
}
return true;
});
const books = [];
const files = {};
app.use(async (ctx, next) => {
ctx.response.set({
'Access-Control-Allow-Origin': '*',
});
await next();
});
router.get('/api/books', async (ctx, next) => {
ctx.body = JSON.stringify(books);
await next();
});
router.get('/api/books/:id', async (ctx, next) => {
const book = books.find((item) => item.key === ctx.params.id);
if (!book) {
ctx.throw(404);
}
ctx.body = JSON.stringify(book)
await next();
});
router.get('/api/books/:id/download', async (ctx, next) => {
const file = files[ctx.params.id];
if (!file) {
ctx.throw(404);
}
ctx.response.set('content-type', file.type);
ctx.body = fs.createReadStream(file.path);
await next();
});
router.post('/api/user/login', async (ctx, next) => {
const message = JSON.parse(ctx.request.body);
const users = [
{ id: 1, mail: 'bropet@mail.ru', pass: '123' },
{ id: 2, mail: 'test@test.com', pass: 'test' },
];
let response = users.filter(
(elem) => !!(elem.mail === message.mail && elem.pass === message.pass),
);
response =
response.length !== 0
? { id: response[0].id, mail: response[0].mail }
: { message: 'Неправильая почта или пароль' };
ctx.body = JSON.stringify(response);
await next();
});
router.post('/api/books', async (ctx, next) => {
const book = ctx.request.body;
books.push(book);
files[book.key] = ctx.request.files.fileBook;
ctx.body = JSON.stringify(book);
await next();
});
router.get('/api/favotites/books', async (ctx, next) => {
ctx.body = JSON.stringify(
books.filter((element) => element.favorite === 'true'),
);
await next();
});
router.post('/api/favorites/books/:id', async (ctx, next) => {
books.forEach((element) => {
if (element.key === ctx.params.id) {
const elem = element;
elem.favorite = 'true';
}
});
ctx.body = 'ok';
await next();
});
router.del('/api/favorites/books/:id', async (ctx, next) => {
books.forEach((element) => {
if (element.key === ctx.params.id) {
const elem = element;
elem.favorite = '';
}
});
ctx.body = 'ok';
await next();
});
app.use(router.routes()).use(router.allowedMethods());
const server = http.createServer(app.callback());
const port = process.env.PORT || 7071;
server.listen(port);