최현영

whereur

1 -var express = require('express') 1 +var express = require('express');
2 - , http = require('http') 2 +const request = require('request');
3 - , path = require('path'); 3 +const bodyParser = require('body-parser');
4 - 4 +var app = express();
5 -var request = require('request'); 5 +var static = require('serve-static');
6 -
7 -var bodyParser = require('body-parser')
8 - , cookieParser = require('cookie-parser')
9 - , static = require('serve-static')
10 - , errorHandler = require('errorhandler');
11 -
12 6
13 -var expressSession = require('express-session'); 7 +const fs = require('fs');
8 +const path = require('path');
9 +const HTTPS = require('https');
10 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
11 +const TOKEN = 'sqNjA99TptppqvcoVFAnU7Kawsl+s7l+JgnZ7r2is97qrFQraIn0sLQ6mTsIPvXLG7FNK34rvjkXbgwuy0/bgrUn4J5eYPn4H/IH27MdimpiXo/yv+obIN9IKlqR+EF2gLmuXSxMU/LWzyGxCk1G0gdB04t89/1O/w1cDnyilFU='
12 +//const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
13 +//const PAPAGO_ID = 'AH9WUFCtTrz9xnJ6dpxB'
14 +//const PAPAGO_SECRET = 'um7UxSvbxD'
14 15
15 -var app = express(); 16 +const domain = "www.chyoss.tk"
17 +const sslport = 23023;
16 18
19 +app.use(bodyParser.json());
17 20
18 -app.set('port', process.env.PORT || 3000); 21 +var isstart = true;
22 +//전역변수 선언
19 23
20 // body-parser를 이용해 application/x-www-form-urlencoded 파싱 24 // body-parser를 이용해 application/x-www-form-urlencoded 파싱
21 app.use(bodyParser.urlencoded({ extended: false })) 25 app.use(bodyParser.urlencoded({ extended: false }))
...@@ -26,147 +30,247 @@ app.use(bodyParser.json()) ...@@ -26,147 +30,247 @@ app.use(bodyParser.json())
26 // public 폴더를 static으로 오픈 30 // public 폴더를 static으로 오픈
27 app.use('/public', static(path.join(__dirname, 'public'))); 31 app.use('/public', static(path.join(__dirname, 'public')));
28 32
29 -// cookie-parser 설정 33 +var router = express.Router();
30 -app.use(cookieParser());
31 -
32 -// 세션 설정
33 -app.use(expressSession({
34 - secret:'my key',
35 - resave:true,
36 - saveUninitialized:true
37 -}));
38 34
35 +router.route('/process/maprealtime').get(function(req, res){
36 + console.log('process/mapreatime 호출됨');
37 + res.end();
38 +});
39 39
40 -// 라우터 객체 참조
41 -var router = express.Router();
42 40
43 router.route('/process/maprealtime').get(function(req, res){ 41 router.route('/process/maprealtime').get(function(req, res){
44 console.log('process/mapreatime 호출됨'); 42 console.log('process/mapreatime 호출됨');
43 + res.end();
45 }); 44 });
46 45
47 router.route('/process/construction').get(function(req, res){ 46 router.route('/process/construction').get(function(req, res){
48 console.log('process/maprealconstruction 호출됨'); 47 console.log('process/maprealconstruction 호출됨');
48 + res.end();
49 }); 49 });
50 50
51 router.route('/process/mapcctv').get(function(req, res){ 51 router.route('/process/mapcctv').get(function(req, res){
52 - console.log('process/mapcctv 호출됨') 52 + console.log('process/mapcctv 호출됨');
53 + res.end();
53 }); 54 });
54 55
55 56
56 app.use('/', router); 57 app.use('/', router);
57 58
58 -var url1 = 'http://data.ex.co.kr/openapi/odtraffic/trafficAmountByCongest'; 59 +app.post('/hook', function (req, res) {
59 -var queryParams1 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/ 60 +
60 -queryParams1 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */ 61 + var eventObj = req.body.events[0];
61 - 62 + var source = eventObj.source;
62 -request({ 63 + var message = eventObj.message;
63 - url: url1 + queryParams1, 64 + // request log
64 - method: 'GET' 65 + console.log('======================', new Date() ,'======================');
65 -}, function (error, response, body) { 66 + console.log('[request]', req.body);
66 - console.log('Status', response.statusCode); 67 + console.log('[request source] ', source);
67 - console.log('Headers', JSON.stringify(response.headers)); 68 + console.log('[request message]', message);
68 - console.log('Reponse received', body); 69 + helloworld(isstart, eventObj);
69 - var obj = JSON.parse(body); 70 + res.sendStatus(200);
70 - console.log(obj);
71 - console.log(obj.list[0])
72 -});
73 -
74 -
75 -
76 -var url2 = 'http://data.ex.co.kr/openapi/safeDriving/forecast';
77 -var queryParams2 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/
78 -queryParams2 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */
79 -
80 -request({
81 - url: url2 + queryParams2,
82 - method: 'GET'
83 -}, function (error, response, body) {
84 - console.log('Status', response.statusCode);
85 - console.log('Headers', JSON.stringify(response.headers));
86 - console.log('Reponse received', body);
87 - var obj = JSON.parse(body);
88 - console.log(obj);
89 - console.log(obj.list[0])
90 -});
91 -
92 -var url3 = 'http://data.ex.co.kr/openapi/trafficapi/trafficAll';
93 -var queryParams3 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/
94 -queryParams3 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */
95 -
96 -request({
97 - url: url3 + queryParams3,
98 - method: 'GET'
99 -
100 -}, function (error, response, body) {
101 - console.log('Status', response.statusCode);
102 - console.log('Headers', JSON.stringify(response.headers));
103 - console.log('Reponse received', body);
104 - var obj = JSON.parse(body);
105 - console.log(obj);
106 - console.log(obj.trafficAll[0]);
107 }); 71 });
108 72
109 - 73 +function helloworld(isstart, eventObj){
110 -var url4 = 'http://data.ex.co.kr/openapi/trafficapi/trafficRegion'; 74 + request.post(
111 -var queryParams4 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/ 75 + {
112 -queryParams4 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */ 76 + url: TARGET_URL,
113 - 77 + headers: {
114 -request({ 78 + 'Authorization': `Bearer ${TOKEN}`
115 - url: url4 + queryParams4, 79 + },
116 - method: 'GET' 80 + json: {
117 - 81 + "replyToken":eventObj.replyToken,
118 -}, function (error, response, body) { 82 + "messages":[
119 - console.log('Status', response.statusCode); 83 + {
120 - console.log('Headers', JSON.stringify(response.headers)); 84 + "type":"text",
121 - console.log('Reponse received', body); 85 + "text":"안녕하세요, 교통정보 알리미입니다."
122 - var obj = JSON.parse(body); 86 + },
123 - console.log(obj); 87 + {
124 - console.log(obj.trafficRegion[0]); 88 + "type":"text",
125 -}); 89 + "text":"무엇이 궁금하신가요??"
126 - 90 + },
127 -var url5 = 'http://data.ex.co.kr/openapi/business/conveniServiceArea'; 91 + {
128 -var queryParams5 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/ 92 + "type":"text",
129 -queryParams5 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */ 93 + "text":"https://www.chyoss.tk:23023/public/maprealtime.html"
130 - 94 + }
131 -request({ 95 + ]
132 - url: url5 + queryParams5, 96 + }
133 - method: 'GET' 97 + },(error, response, body) => {
134 - 98 + console.log(body)
135 -}, function (error, response, body) { 99 + });
136 - console.log('Status', response.statusCode); 100 +}
137 - console.log('Headers', JSON.stringify(response.headers)); 101 +
138 - console.log('Reponse received', body); 102 +
139 - var obj = JSON.parse(body); 103 +
140 - console.log(obj); 104 +
141 - console.log(obj.list[0]); 105 +// function selectlan(language){
142 -}); 106 +// if( language == '일본어'){
143 - 107 +
144 - 108 +// return 'target=ja';
145 -var url6 = 'http://openapi.its.go.kr/api/NTrafficInfo' 109 +// }
146 -var queryParams6 = '?' + encodeURIComponent('key') + '=1590913608992'; /* Service Key*/ 110 +// else if (language == '영어'){
147 -queryParams6 += '&' + encodeURIComponent('ReqType') + '=' + encodeURIComponent('2'); 111 +
148 - /* 112 +// return 'target=en';
149 -http://openapi.its.go.kr/api/NTrafficInfo?key=test&ReqType=2&MinX=126.800000&MaxX=127.890000&MinY=34.900000 &MaxY=35.100000*/ 113 +// }
150 - 114 +// else if (language == '프랑스어'){
151 - 115 +
152 -request({ 116 +// return 'target=fr';
153 - url: url6 + queryParams6, 117 +// }
154 - method: 'GET' 118 +// }
155 - 119 +
156 -}, function (error, response, body) { 120 +// function is(lantext){
157 - console.log('Status', response.statusCode); 121 +// if (lantext == '일본어' || lantext == '영어' || lantext == '프랑스어'){
158 - console.log('Headers', JSON.stringify(response.headers)); 122 +// return true;
159 - console.log('Reponse received', body); 123 +// }else{
160 - // var obj = JSON.parse(body); 124 +// return false;
161 - // console.log(obj); 125 +// }
162 - // console.log(obj.list[0]); 126 +// }
163 -}); 127 +
164 - 128 +
165 - 129 +
166 -// Express 서버 시작 130 +// function trans(replyToken, selectinput ,message) {
167 -http.createServer(app).listen(app.get('port'), function(){ 131 +// //var select = selectlan(lan);
168 - console.log('서버가 시작되었습니다. 포트 : ' + app.get('port')); 132 +// var selectin = selectinput
169 - 133 +// request.post(
170 - 134 +// {
135 +// url: PAPAGO_URL,
136 +// headers: {
137 +// 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
138 +// 'X-Naver-Client-Id': `${PAPAGO_ID}`,
139 +// 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
140 +// },
141 +// body: selectin + message,
142 +// json:true
143 +// },(error, response, body) => {
144 +// if(!error && response.statusCode == 200) {
145 +// console.log(body.message);
146 +// var transMessage = body.message.result.translatedText;
147 +// request.post(
148 +// {
149 +// url: TARGET_URL,
150 +// headers: {
151 +// 'Authorization': `Bearer ${TOKEN}`
152 +// },
153 +// json: {
154 +// "replyToken":replyToken,
155 +// "messages":[
156 +// {
157 +// "type":"text",
158 +// "text":transMessage
159 +// }
160 +// ]
161 +// }
162 +// },(error, response, body) => {
163 +// console.log(body)
164 +// });
165 +// }
166 +// });
167 +
168 +// }
169 +
170 +
171 +// var url1 = 'http://data.ex.co.kr/openapi/odtraffic/trafficAmountByCongest';
172 +// var queryParams1 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/
173 +// queryParams1 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */
174 +
175 +// request({
176 +// url: url1 + queryParams1,
177 +// method: 'GET'
178 +// }, function (error, response, body) {
179 +// console.log('Status', response.statusCode);
180 +// console.log('Headers', JSON.stringify(response.headers));
181 +// console.log('Reponse received', body);
182 +// var obj = JSON.parse(body);
183 +// console.log(obj);
184 +// console.log(obj.list[0])
185 +// });
186 +
187 +
188 +
189 +// var url2 = 'http://data.ex.co.kr/openapi/safeDriving/forecast';
190 +// var queryParams2 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/
191 +// queryParams2 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */
192 +
193 +// request({
194 +// url: url2 + queryParams2,
195 +// method: 'GET'
196 +// }, function (error, response, body) {
197 +// console.log('Status', response.statusCode);
198 +// console.log('Headers', JSON.stringify(response.headers));
199 +// console.log('Reponse received', body);
200 +// var obj = JSON.parse(body);
201 +// console.log(obj);
202 +// console.log(obj.list[0])
203 +// });
204 +
205 +// var url3 = 'http://data.ex.co.kr/openapi/trafficapi/trafficAll';
206 +// var queryParams3 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/
207 +// queryParams3 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */
208 +
209 +// request({
210 +// url: url3 + queryParams3,
211 +// method: 'GET'
212 +
213 +// }, function (error, response, body) {
214 +// console.log('Status', response.statusCode);
215 +// console.log('Headers', JSON.stringify(response.headers));
216 +// console.log('Reponse received', body);
217 +// var obj = JSON.parse(body);
218 +// console.log(obj);
219 +// console.log(obj.trafficAll[0]);
220 +// });
221 +
222 +
223 +// var url4 = 'http://data.ex.co.kr/openapi/trafficapi/trafficRegion';
224 +// var queryParams4 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/
225 +// queryParams4 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */
226 +
227 +// request({
228 +// url: url4 + queryParams4,
229 +// method: 'GET'
230 +
231 +// }, function (error, response, body) {
232 +// console.log('Status', response.statusCode);
233 +// console.log('Headers', JSON.stringify(response.headers));
234 +// console.log('Reponse received', body);
235 +// var obj = JSON.parse(body);
236 +// console.log(obj);
237 +// console.log(obj.trafficRegion[0]);
238 +// });
239 +
240 +// var url5 = 'http://data.ex.co.kr/openapi/business/conveniServiceArea';
241 +// var queryParams5 = '?' + encodeURIComponent('key') + '=4365330273'; /* Service Key*/
242 +// queryParams5 += '&' + encodeURIComponent('type') + '=' + encodeURIComponent('json'); /* */
243 +
244 +// request({
245 +// url: url5 + queryParams5,
246 +// method: 'GET'
247 +
248 +// }, function (error, response, body) {
249 +// console.log('Status', response.statusCode);
250 +// console.log('Headers', JSON.stringify(response.headers));
251 +// console.log('Reponse received', body);
252 +// var obj = JSON.parse(body);
253 +// console.log(obj);
254 +// console.log(obj.list[0]);
255 +// });
256 +
257 +
258 +
259 +
260 +
261 +
262 +try {
263 + const option = {
264 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
265 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
266 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
267 + };
268 +
269 + HTTPS.createServer(option, app).listen(sslport, () => {
270 + console.log(`[HTTPS] Server is started on port ${sslport}`);
171 }); 271 });
272 + } catch (error) {
273 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
274 + console.log(error);
275 + }
172 276
...\ No newline at end of file ...\ No newline at end of file
......
1 +{
2 + "_from": "https",
3 + "_id": "https@1.0.0",
4 + "_inBundle": false,
5 + "_integrity": "sha1-PDfHrhqO65ZpBKKtHpdaGUt+06Q=",
6 + "_location": "/https",
7 + "_phantomChildren": {},
8 + "_requested": {
9 + "type": "tag",
10 + "registry": true,
11 + "raw": "https",
12 + "name": "https",
13 + "escapedName": "https",
14 + "rawSpec": "",
15 + "saveSpec": null,
16 + "fetchSpec": "latest"
17 + },
18 + "_requiredBy": [
19 + "#USER",
20 + "/"
21 + ],
22 + "_resolved": "https://registry.npmjs.org/https/-/https-1.0.0.tgz",
23 + "_shasum": "3c37c7ae1a8eeb966904a2ad1e975a194b7ed3a4",
24 + "_spec": "https",
25 + "_where": "/Users/hyunyeong/Desktop/WhereUr/WhereUR/whereur",
26 + "author": {
27 + "name": "hardus van der berg",
28 + "email": "hardus@sunfork.com",
29 + "url": "http://www.sunfork.com"
30 + },
31 + "bundleDependencies": false,
32 + "deprecated": false,
33 + "description": "https mediation",
34 + "keywords": [
35 + "https",
36 + "mediation"
37 + ],
38 + "license": "ISC",
39 + "main": "index.js",
40 + "name": "https",
41 + "scripts": {
42 + "test": "echo \"Error: no test specified\" && exit 1"
43 + },
44 + "version": "1.0.0"
45 +}
...@@ -424,6 +424,11 @@ ...@@ -424,6 +424,11 @@
424 "sshpk": "^1.7.0" 424 "sshpk": "^1.7.0"
425 } 425 }
426 }, 426 },
427 + "https": {
428 + "version": "1.0.0",
429 + "resolved": "https://registry.npmjs.org/https/-/https-1.0.0.tgz",
430 + "integrity": "sha1-PDfHrhqO65ZpBKKtHpdaGUt+06Q="
431 + },
427 "iconv-lite": { 432 "iconv-lite": {
428 "version": "0.4.24", 433 "version": "0.4.24",
429 "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", 434 "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
21 "express-error-handler": "^1.1.0", 21 "express-error-handler": "^1.1.0",
22 "express-session": "^1.17.1", 22 "express-session": "^1.17.1",
23 "http": "0.0.1-security", 23 "http": "0.0.1-security",
24 + "https": "^1.0.0",
24 "mongoose": "^5.9.16", 25 "mongoose": "^5.9.16",
25 "path": "^0.12.7", 26 "path": "^0.12.7",
26 "request": "^2.88.2" 27 "request": "^2.88.2"
......