-
Notifications
You must be signed in to change notification settings - Fork 0
/
naverTranslatorServer.js
91 lines (84 loc) · 2.43 KB
/
naverTranslatorServer.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
// Naver translator server-side communication.
// This requires a server side module due to CORS. Browsers can't directly contact Naver's translator.
// We'll handle that here and pass it back to the browser.
//
//○ → curl --data "query=help%20me&srcLang=en&tarLang=ko" http://translate.naver.com/translate.dic
//
//
var http = require('http');
var querystring = require('querystring');
var url = require('url');
var request = require('request');
function parse(body) {
var result = {};
try{
result = JSON.parse(body);
} catch(e) {
console.log('caught error in Naver response: ' + e);
result.translatedMessage = 'Error in Naver Translation';
result.error = e; //error in the above string(in this case,yes)!
} finally {
return result;
};
};
function getTranslation(clientReq, clientRes, next) {
// parse client request
//console.log(clientReq.body());
var query = querystring.parse(url.parse(clientReq.url).query);
console.log(query);
var message = query.message;
var to = query.to;
var from = query.from;
if (from === undefined || to === undefined || message === undefined) {
// return 400 Bad Request
return400(clientRes);
return;
}
// send to naver and reply
var requestContent = {
query: message,
srcLang: from,
tarLang: to
};
console.log(requestContent);
request.post('http://translate.naver.com/translate.dic',
{ form: requestContent },
function( error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
var parsedData = parse(body);
var ourResponse = {
error: parsedData.error,
translatedMessage: parsedData.resultData,
from: requestContent.srcLang,
to: requestContent.tarLang,
naverDir: parsedData.dir,
naverResultCode: parsedData.resultCode
}
console.log('responding: ' + ourResponse);
clientRes.writeHead(200, {"Content-Type": "application/json"});
clientRes.write( JSON.stringify(ourResponse) );
clientRes.end();
} else {
debugger;
}
}
);
return;
}
function return400(res) {
res.writeHead(400);
res.end();
}
function routeMatchingPath(req, res, next) {
console.log(req.url);
if (req.url.match(/^\/getNaverTranslation/i) != null) {
getTranslation(req, res, next);
} else {
next();
}
};
module.exports = {
getTranslation: getTranslation,
route: routeMatchingPath
} ;