-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
28 lines (25 loc) · 887 Bytes
/
index.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
const translator = require('./translator');
const {send} = require('micro');
module.exports = async (req, res) => {
if (req.url === '/') {
return send(res, 500, {
error: true,
message: 'Please use the site via: /[countrycode]/[message]'
});
}
if (req.url !== '/favicon.ico') {
const url = req.url.slice(1);
const splitUrl = url.split('/', 2);
const languageTo = splitUrl[0];
if (languageTo === 'jp') {
return send(res, 500, {
error: true,
message: 'If you\'re looking to translate into japanese, you must use ja instead'
})
}
const text = splitUrl[1];
const decodedText = decodeURIComponent(text);
const translated = await translator(decodedText, languageTo);
return send(res, 200, translated);
}
};