-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
132 lines (120 loc) · 4.73 KB
/
server.ts
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
const http = require('http');
const glossary = [
{
id: '851112',
title: 'Lokalise',
description: 'This is a project name and it should not be translated. You can see this, as it\'s been added to the Glossary.',
caseSensitive: false,
translatable: false,
forbidden: false,
createdAt: '2021-06-11T15:01:56+02:00',
createdBy: { 'id': '111884', 'fullName': 'Jane Doe' },
},
{
id: '851113',
title: 'Mercedes-Benz',
description: 'It is a German automotive brand.',
caseSensitive: true,
translatable: false,
forbidden: false,
createdAt: '2021-05-11T15:01:56+02:00',
createdBy: { 'id': '111884', 'fullName': 'Jane Doe' },
},
{
id: '851114',
title: 'VAT number',
description: 'An identifier used in many countries, including the countries of the European Union, for value added tax purposes',
caseSensitive: true,
translatable: true,
forbidden: false,
createdAt: '2021-05-11T15:01:56+02:00',
createdBy: { 'id': '111884', 'fullName': 'Jane Doe' },
},
{
id: '851115',
title: 'Superman',
description: 'This is a superhero character who first appeared in American comic books published by DC Comics.',
caseSensitive: true,
translatable: true,
forbidden: true,
createdAt: '2021-04-12T15:01:56+02:00',
createdBy: { 'id': '111884', 'fullName': 'Jane Doe' },
},
{
id: '851116',
title: 'Redux',
description: 'A Predictable State Container for JS Apps',
caseSensitive: true,
translatable: false,
forbidden: false,
createdAt: '2021-03-12T15:01:56+02:00',
createdBy: { 'id': '111884', 'fullName': 'Jane Doe' },
},
{
id: '851117',
title: 'React',
description: 'A JavaScript library for building user interfaces.',
caseSensitive: true,
translatable: false,
forbidden: false,
createdAt: '2021-02-12T15:01:56+02:00',
createdBy: { 'id': '111884', 'fullName': 'Jane Doe' },
},
{
id: '851118',
title: 'WebKit',
description: 'A is the web browser engine used by Safari, Mail, App Store, and many other apps on macOS, iOS, and Linux.',
caseSensitive: true,
translatable: false,
forbidden: false,
createdAt: '2021-01-12T15:01:56+02:00',
createdBy: { 'id': '111884', 'fullName': 'Jane Doe' },
},
{
id: '851119',
title: 'Prettier',
description: 'An opinionated code formatter.',
caseSensitive: true,
translatable: false,
forbidden: false,
createdAt: '2021-01-12T15:01:56+02:00',
createdBy: { 'id': '111884', 'fullName': 'Jane Doe' },
},
{
id: '851120',
title: 'Flexbox',
description: 'The Flexible Box Module, usually referred to as flexbox, was designed as a one-dimensional layout model, and as a method that could offer space distribution between items in an interface and powerful alignment capabilities.',
caseSensitive: false,
translatable: false,
forbidden: false,
createdAt: '2021-10-11T15:01:56+02:00',
createdBy: { 'id': '111884', 'fullName': 'Jane Doe' },
},
];
const searchGlossaryItem = (searchPhrase) => ({ title, description }) => {
const lowercaseTitle = title.toLocaleLowerCase();
const lowercaseDescription = description.toLocaleLowerCase();
const searchWords = searchPhrase.toLocaleLowerCase().split(' ');
const isInTitle = !!searchWords.filter((word) => lowercaseTitle.includes(word)).length;
const isInDescription = !!searchWords.filter((word) => lowercaseDescription.includes(word)).length;
return isInTitle || isInDescription;
}
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, GET',
};
const port = 5000;
const server = http.createServer(async (req, res) => {
const location = new URL(req.url, `http://localhost:${port}`);
if (req.method === 'GET' && '/api/glossary' === location.pathname.replace(/\/+$/, '')) {
const searchPhrase = location.searchParams.get('search') ?? '';
const searchGlossaryItemWithPhrase = searchGlossaryItem(searchPhrase);
const response = glossary.filter(searchGlossaryItemWithPhrase);
res.writeHead(200, { ...headers, 'Content-Type': 'application/json' });
res.end(JSON.stringify(response));
} else {
res.writeHead(404, { ...headers, 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Route not found!' }));
}
});
server.listen(port, () => console.log(`Server listening on port ${port}.`));