이찬

DB 연결수정, 서버 시간 비교 코드 추가

Showing 1 changed file with 91 additions and 19 deletions
1 -const port = process.env.PORT || 5000; 1 +const fs = require('fs');
2 -const http = require('http'); 2 +const http = require('http');
3 const express = require('express'); 3 const express = require('express');
4 +const bodyParser = require('body-parser');
4 const path = require('path'); 5 const path = require('path');
5 -const fs = require('fs'); // 파일 읽기, 쓰기 등 을 할 수 있는 모듈
6 const app = express(); 6 const app = express();
7 +const port = process.env.PORT || 5000;
7 const data = fs.readFileSync('./database.json'); 8 const data = fs.readFileSync('./database.json');
8 const conf = JSON.parse(data); 9 const conf = JSON.parse(data);
9 const mysql = require('mysql'); 10 const mysql = require('mysql');
11 +var cookieSession = require('cookie-session')
12 +
13 +app.use(bodyParser.json());
14 +app.use(bodyParser.urlencoded({ extended: true }));
10 15
11 app.use('/js', express.static(__dirname + '/js')); 16 app.use('/js', express.static(__dirname + '/js'));
12 app.use('/css', express.static(__dirname + '/css')); 17 app.use('/css', express.static(__dirname + '/css'));
13 app.use('/html', express.static(__dirname + '/html')); 18 app.use('/html', express.static(__dirname + '/html'));
14 app.use('/img', express.static(__dirname + '/img')); 19 app.use('/img', express.static(__dirname + '/img'));
15 20
21 +var count = 0;
22 +
23 +app.set('trust proxy', 1);
24 +
25 +app.use(cookieSession({
26 + name: 'session',
27 + keys: ['key1', 'key2']
28 +}));
29 +
16 const connection = mysql.createConnection({ 30 const connection = mysql.createConnection({
17 31
18 host: conf.host, 32 host: conf.host,
...@@ -31,29 +45,87 @@ connection.connect(); ...@@ -31,29 +45,87 @@ connection.connect();
31 45
32 let DBdata = []; 46 let DBdata = [];
33 47
34 -app.get('/', (req, res)=>{ 48 +function addZeroMonth(date) {
35 - fs.readFile(path.join(__dirname,'html', 'index.html'), (err, data)=>{ 49 + if ((date.getMonth() + 1) < 10) return `0${date.getMonth() + 1}`;
36 - res.writeHead(200,{'Content-Type':'text/html'}); 50 + else return `${date.getMonth() + 1}`;
37 - res.end(data); 51 +}
38 - }) 52 +
39 -}) 53 +function addZeroDate(date) {
40 -app.get('/detail.html', (req, res)=>{ 54 + if (date.getDate() < 10) return `0${date.getDate()}`;
41 - fs.readFile(path.join(__dirname,'html', 'detail.html'), (err, data)=>{ 55 + else return `${date.getDate()}`;
42 - res.writeHead(200,{'Content-Type':'text/html'}); 56 +}
57 +
58 +var date = new Date();
59 +function server_time() {
60 + date.setSeconds(date.getSeconds() + 1);
61 + var hours = date.getHours();
62 + var minutes = date.getMinutes();
63 + var seconds = date.getSeconds();
64 + if (hours < 10) {
65 + hours = "0" + hours;
66 + }
67 + if (minutes < 10) {
68 + minutes = "0" + minutes;
69 + }
70 + if (seconds < 10) {
71 + seconds = "0" + seconds;
72 + }
73 + console.log(hours + ":" + minutes + ":" + seconds);
74 +}
75 +
76 +app.get('/', (req, res) => {
77 + var date = new Date();
78 + var dateString = `${date.getFullYear()}-${addZeroMonth(date)}-${addZeroDate(date)}`;
79 + var today = date.getYear() + " " + date.getMonth() + " " + date.getDate();
80 +
81 + setInterval(() => {
82 + date.setSeconds(date.getSeconds() + 1);
83 + var hours = date.getHours();
84 + var minutes = date.getMinutes();
85 + var seconds = date.getSeconds();
86 + console.log(hours + ":" + minutes + ":" + seconds);
87 + if (hours === 23 && minutes === 59 && seconds === 59) {
88 + //수정해야하는 DB 푸쉬
89 + DBdata.push({
90 + "date": dateString,
91 + "value": count
92 + });
93 + count = 0;
94 + }
95 + }, 1000);
96 + console.log(req.session.lastVisit);
97 + if (req.session.lastVisit != today) {
98 + req.session.lastVisit = today;
99 + count++;
100 + }
101 +
102 + fs.readFile(path.join(__dirname, 'html', 'index.html'), (err, data) => {
103 + res.writeHead(200, { 'Content-Type': 'text/html' });
104 + console.log(count);
43 res.end(data); 105 res.end(data);
44 }) 106 })
45 -}) 107 +});
46 108
47 -app.get('/statistics.html', (req, res)=>{ 109 +app.get('/detail.html', (req, res) => {
48 - /* 110 + fs.readFile(path.join(__dirname, 'html', 'detail.html'), (err, data) => {
111 + res.writeHead(200, { 'Content-Type': 'text/html' });
112 + res.end(data);
113 + });
114 +});
115 +app.get('/api/visiters', (req,res)=>{
49 connection.query( 116 connection.query(
50 'SELECT * FROM visiter', 117 'SELECT * FROM visiter',
51 (err, rows, fields) => { 118 (err, rows, fields) => {
52 - DBdata.push(rows); 119 + res.send(rows);
53 } 120 }
54 - )*/ 121 + )
55 - fs.readFile(path.join(__dirname,'html', 'statistics.html'), (err, data)=>{ 122 +})
56 - res.writeHead(200,{'Content-Type':'text/html'}); 123 +
124 +
125 +app.get('/statistics.html', (req, res) => {
126 +
127 + fs.readFile(path.join(__dirname, 'html', 'statistics.html'), (err, data) => {
128 + res.writeHead(200, { 'Content-Type': 'text/html' });
57 res.end(data); 129 res.end(data);
58 }) 130 })
59 }) 131 })
......