장주현

add papago-api function

1 const express = require('express'); 1 const express = require('express');
2 +const request = require('request');
2 const app = express(); 3 const app = express();
4 +const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
5 +const client_id = 'cIZBOTfISMHEyygh_NHp';
6 +const client_secret = '8D32UNGgYp';
3 const PORT = process.env.PORT || 3000; 7 const PORT = process.env.PORT || 3000;
4 8
5 //const indexRoute = require("./routes/index"); 9 //const indexRoute = require("./routes/index");
...@@ -13,6 +17,7 @@ app.use(express.static(__dirname + '/public')); ...@@ -13,6 +17,7 @@ app.use(express.static(__dirname + '/public'));
13 app.get('/', function(req, res){ 17 app.get('/', function(req, res){
14 //res.send('hello world'); 18 //res.send('hello world');
15 res.render('index'); 19 res.render('index');
20 +
16 }); 21 });
17 22
18 //app.use('/', indexRoute); 23 //app.use('/', indexRoute);
...@@ -20,3 +25,22 @@ app.use(express.static(__dirname + '/public')); ...@@ -20,3 +25,22 @@ app.use(express.static(__dirname + '/public'));
20 app.listen(PORT, function(){ 25 app.listen(PORT, function(){
21 console.log('listen',PORT); 26 console.log('listen',PORT);
22 }); 27 });
28 +
29 +function trans(message, totrans){
30 + request.post(
31 + {
32 + url: PAPAGO_URL,
33 + headers: {
34 + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
35 + 'X-Naver-Client-Id': `${PAPAGO_ID}`,
36 + 'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
37 + },
38 + body: 'source=ko&target=en&text=' + message,
39 + json:true
40 + },(error, response, body) => {
41 + if(!error && response.statusCode == 200) {
42 + console.log(body.message);
43 + totrans = body.message.result.translatedText;
44 + }
45 + });
46 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -3,6 +3,7 @@ const router = express.Router(); ...@@ -3,6 +3,7 @@ const router = express.Router();
3 3
4 router.get('/', function(req, res){ 4 router.get('/', function(req, res){
5 res.render('index'); 5 res.render('index');
6 + console.log("trtt")
6 }); 7 });
7 8
8 module.exports = router; 9 module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
......