cliConnection.js
2.25 KB
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
const textAnalytic=require('./textAnalystic');
const search=require('./search');
const machineRead = require('./machineRead');
/**
* @param req - request
* @param req.body.data - client에서 보내는 데이터 req.body.data.text에 검색할 문장을 담아야 합니다
* @description client와 데이터를 받아 통신하는 함수입니다
*/
const cliConnection = async ( req, res ) => {
let clientData = {},
analyzeData = {},
searchData = [];
let timer = setTimeout( ()=>{
try {
throw new Error( "TIMEOUT ERROR" );
} catch ( err ) {
res.json( { "return_code" : -1, "error_code" : err.message } );
res.status( 504 ); //Gateway Timeout
return false;
}
}, 10000 );
try {
clientData = req.body.data;
//clientData = req.body.data;
if( !clientData.text.replace( /\s/g, '' ).length ) {
throw new Error( "client text empty" );
}
}
catch ( err ) {
console.log( err );
res.json( { "return_code" : -1, "error_code" : err.message } );
res.status( 403 );
return false;
}
try {
analyzeData = await textAnalytic( clientData );
}
catch ( err ) {
console.log( err );
res.json( { "return_code" : -1, "error_code" : err.message } );
res.status( 502 );
return false;
}
try {
searchData = await Promise.all( [ search.naver( analyzeData.keywordText ), search.google( analyzeData.keywordText ) ] );
}
catch ( err ) {
console.log( err );
res.json( { "return_code" : -1, "error_code" : err.message } );
res.status( 503 );
return false;
}
try {
searchData = searchData[ 0 ].concat( searchData[ 1 ] );
searchData = await machineRead( searchData, analyzeData.keywordText );
}
catch ( err ) {
console.log( err );
res.json( { "return_code" : -1, "error_code" : err.message } );
res.status( 502 );
return false;
}
analyzeData.searchResults = searchData;
res.send( { "return_code" : 0, "return_data" : analyzeData } );
res.status( 200 );
clearTimeout(timer);
return true;
};
module.exports = cliConnection;