index.js 2.44 KB
var express = require('express');
var router = express.Router();
var connection = require('./db');
var fs = require('fs');
var ejs = require('ejs');


var mapPage = fs.readFileSync('routes/mapPage.ejs', 'utf8');
var findPage = fs.readFileSync('routes/findPage.ejs', 'utf-8');
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 * 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, results[i].start_day, results[i].where]);
        var page = ejs.render(mapPage, {
          title: "show data",
          data: show_list,
          dataNum: dataNum,
        });

      }
      res.send(page);
    }

  });
});

router.get('/findpage', (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 * 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, results[i].start_day, results[i].where]);
        var page = ejs.render(findPage, {
          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('/send', function (req, res, next) {
  res.render('send.html', { title: 'Send message' });
  console.log('카카오톡 공유 메시지 접속 성공');
})



module.exports = router;