-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (51 loc) · 1.04 KB
/
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
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
const nomlish = require("nomlish");
const express = require("express");
const app = express();
const port = 3000;
app.listen(port);
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
//稼働状況
app.get("/api/ping", (req, res) => {
const status = {
"status": "OK"
};
res.status(200).json(status);
});
//翻訳
app.post("/api/translate", (req, res) => {
let level;
if (!req.body.level) {
level = 2;
} else if (typeof req.body.level !== 'number') {
level = 2;
} else {
level = req.body.level;
}
const beforText = req.body.text;
nomlish.translate(beforText, level).then((result) => {
let response;
if (result) {
//正常
response = {
"status": 0,
"level": level,
"result": result
};
} else {
//翻訳不能
response = {
"status": 1
};
}
res.status(200).json(response);
}).catch(() => {
//サーバーエラー
const response = {
"status": 100
};
res.status(500).json(response);
});
});