index.js 2 KB
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var fs = require('fs');
var ejs = require('ejs');

//추후 key 암호화 필요함 
var connection = mysql.createConnection({
  "host": "culturegallery.cm2bwcuyukrm.us-east-1.rds.amazonaws.com",
  "user": "root",
  "password": "dldbwp1207",
  "port": "3306",
  "database": "showdata"
})
connection.connect();

var mapPage = fs.readFileSync('routes/mapPage.ejs', 'utf8');
var dataNum = 0;

router.get('/mappage', (req, res) => {
  connection.query('SELECT COUNT (*) AS cnt FROM SHOW_DATA', function (error, results, field) {
    if (error) {
      console.log(error);
    }
    console.log(results[0].cnt);
    dataNum = results[0].cnt;
  });
  ////
  connection.query('SELECT id,oper_name,latitude,longitude FROM SHOW_DATA', function (error, results, field) {
    if (error) {
      console.log(error);
    } else {
      var show_list = [];
      for (var i = 0; i < dataNum; i++) {
        show_list.push([results[i].id, results[i].oper_name, results[i].latitude, results[i].longitude]);
        var page = ejs.render(mapPage, {
          title: "show data",
          data: show_list,
          dataNum: dataNum,
        });

      }
      res.send(page);
    }

  });
});


//루트 페이지 (메인페이지)에서 실행됨 : title 할당하고 main.html 띄워줌.
router.get('/', function (req, res, next) {
  res.render('main.html', { title: 'Culture Gallery' });
  console.log('main 접속 성공');
});

/* GET home page. */
router.get('/login', function (req, res, next) {
  res.render('login.html', { title: 'Login' });
  console.log('로그인 페이지 접속 성공');
});
router.get('/search', function (req, res, next) {
  res.render('search.html', { title: '검색 결과' });
  console.log('검색 결과 페이지 접속 성공');
})
router.get('/send', function (req, res, next) {
  res.render('send.html', { title: 'Send message' });
  console.log('카카오톡 공유 메시지 접속 성공');
})

module.exports = router;