app.js 7.67 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 = 'Client ID';
const PAPAGO_SECRET = 'Client Secret';
//const path = require('path');
//const HTTPS = require('https');
//const domain = "www.chatosscjh.tk"
const sslport = 23023;

//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 addressK, addressE;
var detailaddressK, detailaddressE;
var extraaddressK, extraaddressE;
var zipcode;
var cityE, stateE, streetE;

 app.get('/', function(req, res){
    res.render('index');
     
 });

app.post('/', async function(req,res){
    if(req.body.process == "주소 변환"){
        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);
        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);
        console.log("address: " + addressE);
        if(req.body.os == "amazon"){
            res.render('amazonaddr', { cityE : cityE, stateE : stateE, streetE : streetE, detailaddressE : detailaddressE, extraaddressE : extraaddressE, zipcode : zipcode, os : req.body.os});
        }
        if(req.body.os == "ebay"){
            res.render('ebayaddr', { cityE : cityE, stateE : stateE, streetE : streetE, detailaddressE : detailaddressE, extraaddressE : extraaddressE, zipcode : zipcode, os : req.body.os});
        }
    }
    else if(req.body.process == "환율 계산"){
        var card = req.body.card;
        var credit = req.body.credit;
        var brand = req.body.brand;
        var moneyU = Number(req.body.moneyU);
        var addmoneyU = 0;
        var addpersent = 0;
        var exchange;
        if(brand == "VISA"){
            addpersent += 0.011;
        }
        else if(brand == "MASTER"){
            addpersent += 0.01;
        }
        else if(brand == "American Express"){
            addpersent += 0.01;
        }
        else if(brand == "JCB"){
        }
        else if(brand == "UnionPay"){
            if(card == "KB국민카드"){
                addpersent += 0.008;
            }
        }

        if(card =="KB국민카드"){
            addpersent += 0.0025;
        }
        else if(card =="NH농협카드"){
            if(credit == "신용카드"){
                addpersent += 0.0025;
            }
            else if(credit == "체크카드"){
                addmoneyU += 0.5;
            }
        }
        else if(card =="롯데카드"){
            addpersent += 0.002;
        }
        else if(card =="삼성카드"){
            addpersent += 0.002;
        }
        else if(card =="신한카드"){
            if(credit == "신용카드"){
                addpersent += 0.0018;
            }
            else if(credit == "체크카드"){
                addpersent += 0.002;
            }
        }
        else if(card =="씨티카드"){
            addpersent += 0.0025;
        }
        else if(card =="우리카드"){
            if(credit == "신용카드"){
                addpersent += 0.003;
            }
            else if(credit == "체크카드"){
                addmoneyU += 0.5;
            }
        }
        else if(card =="현대카드"){
            if(credit == "신용카드"){
                addpersent += 0.0018;
            }
            else if(credit == "체크카드"){
            }
        }
        else if(card =="하나카드"){
            if(credit == "신용카드"){
                addpersent += 0.002;
            }
            else if(credit == "체크카드"){
                addmoneyU += 0.5;
            }
        }
        else if(card =="BC카드"){
        }

        request({
            encoding: "utf-8",
            method: "GET",
            uri : "https://api.exchangeratesapi.io/2020-06-10?base=USD&symbols=KRW"
        }, function(err,res,html){
            exchange = Number(html.substring(16,23));
        })
        setTimeout(function(){
            var result = (moneyU + addmoneyU) * (1 + addpersent) * exchange;
            console.log(result);
            res.send("원화: "+ result + "원");
        },300);
    }
});

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;
            }
        });
};

app.listen(sslport, function(){
     console.log('listen',sslport);
 });

//try {
//    const option = {
//      ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
//      key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
//      cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
//    };
//  
//    HTTPS.createServer(option, app).listen(sslport, () => {
//      console.log(`[HTTPS] Server is started on port ${sslport}`);
//    });
//  } catch (error) {
//    console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
//    console.log(error);
//  }