category.js 2.04 KB
var express = require('express');
var router = express.Router();
var request = require('request');
require('dotenv').config();

/* GET home page. */
let SIGUN_CODE;
let SIGUNDONG_CODE;

// 사용자가 index 페이지에서 주소를 입력하고 넘어오면 
router.post('/', function(req, res) {
    function callback(cb){
        cb();
    }
    console.log(req.body);
    let userLocation = req.body.userLocation;
    // let userSi = req.body.si
    // let userDong = req.body.dong;
    let xyList = [];
    console.log(userLocation);
    // console.log(req);
    let YOUR_API_KEY = process.env.KAKAO_APIKEY;
    let kakaoOptions = {
        url: 'https://dapi.kakao.com/v2/local/search/address.json',
        method: 'GET',
        headers: {
            'Authorization': `KakaoAK ${YOUR_API_KEY}`
        },
        qs: {
            query: userLocation
        },
        encoding: 'UTF-8',
    }
    request(kakaoOptions, function(err, res, body) {
        if (!err && res.statusCode == 200) {
            var result = JSON.parse(body);
            console.log('result', result);
            xyList.push(result.documents[0].address.x);
            xyList.push(result.documents[0].address.y);
            console.log(xyList);
            
            callback(function(){
                //버스api
            })
            callback(function(){
                //지하철api
            })
            callback(function(){
                //버스+지하철
            })
            
        }
    })
    res.render('category', { 'userLocation': req.body.userLocation });
});

router.get('/food', function(req, res) {
    res.send('This is food');
});
router.get('/school', function(req, res) {
    res.send('This is school');
});
router.get('/park', function(req, res) {
    res.send('This is park');
});
router.get('/transport', function(req, res) {
    res.send('This is transport');
});
router.get('/safe', function(req, res) {
    res.send('This is safe');
});

router.get('/culture', function(req, res) {
    res.send('This is culture');
});

module.exports = router;