app.js
2.19 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
const express = require('express');
const request = require('request');
const fs = require('fs');
const bodyParser = require('body-parser');
const app = express();
const PAPAGO_URL = 'https://openapi.naver.com/v1/papago/n2mt'
const PAPAGO_ID = 'cIZBOTfISMHEyygh_NHp';
const PAPAGO_SECRET = '8D32UNGgYp';
const PORT = process.env.PORT || 3000;
//const indexRoute = require("./routes/index");
app.use(bodyParser.urlencoded({extended: true}));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
//var postcode = require(postcode.v2.html);
//const fs = require('filesystem')
var addressK, addressE;
var detailaddressK, detailaddressE;
var extraaddressK, extraaddressE;
var zipcode;
app.get('/', function(req, res){
//res.send('hello world');
res.render('index');
});
app.get('/', function(req, res){
res.render('index');
});
//app.use('/', indexRoute);
app.post('/', function(req,res){
console.log(req.body);
zipcode = req.body.zipcode;
addressK = req.body.addressK;
detailaddressK = req.body.detailaddressK;
extraaddressK = req.body.extraaddressK;
console.log(addressK);
addressE = trans(addressK, detailaddressE);
//trans(detailaddressK, detailaddressE);
//trans(extraaddressK, extraaddressE);
setTimeout(function(){console.log("set: " +detailaddressE)},500);
console.log("address: " + addressE);
console.log("detail: " + detailaddressE);
});
app.listen(PORT, function(){
console.log('listen',PORT);
});
function trans(message, totrans){
request.post(
{
url: PAPAGO_URL,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Naver-Client-Id': `${PAPAGO_ID}`,
'X-Naver-Client-Secret': `${PAPAGO_SECRET}`
},
body: 'source=ko&target=en&text=' + message,
json:true
},(error, response, body) => {
if(!error && response.statusCode == 200) {
console.log(body.message);
totrans = body.message.result.translatedText;
return totrans;
}
});
};