app.js 4.05 KB
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;
var cityE, stateE, streetE;

 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('/', async function(req,res){
    console.log(req.body);
    zipcode = req.body.zipcode;
    addressK = req.body.addressK;
    addressE = req.body.addressE;
    detailaddressK = req.body.detailaddressK;
    extraaddressK = req.body.extraaddressK;
    extraaddressK = extraaddressK.replace("(", "");
    extraaddressK = extraaddressK.replace(")", "");
    console.log(extraaddressK);
    console.log(addressK);
    //trans(detailaddressK, detailaddressE);
    //trans(extraaddressK, extraaddressE);
    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=' + detailaddressK,
            json:true
        },(error, response, body) => {
            if(!error && response.statusCode == 200) {
                console.log(body.message);
                detailaddressE = body.message.result.translatedText;
            }
        });
    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=' + extraaddressK,
            json:true
        },(error, response, body) => {
            if(!error && response.statusCode == 200) {
                console.log(body.message);
                extraaddressE = body.message.result.translatedText;
            }
        });   
    setTimeout(function(){
        var splitaddressE = addressE.split(", ");
        cityE = splitaddressE[4];
        stateE = splitaddressE[3];
        streetE = splitaddressE[0] + ", " +splitaddressE[1] + ", " + splitaddressE[2];
    },300);
    setTimeout(function(){console.log("set2: " +detailaddressE)},300);
    setTimeout(function(){console.log("set2: " +extraaddressE)},300);
    //var splitaddressE = addressE.split(", ")
    console.log("address: " + addressE);
    res.render('changeaddr', { cityE : cityE, stateE : stateE, streetE : streetE, detailaddressE : detailaddressE, extraaddressE : extraaddressE, zipcode : zipcode, os : req.body.os})
});


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;
                ffu();
                return body.message.result.translatedText;
            }
        });
};