유영빈

오류 및 렌더링 수정 후 병합. 필요없는 파일 제거 요망

1 +const express = require("express");
2 +var app = express();
3 +http = require('http').createServer(app);
4 +port = 23023;
5 +
6 +app.use(express.static(__dirname + "/views"));
7 +const routes = require("./routes/");
8 +const errorController = require("./routes/errorController")
9 +app.use(routes)
10 +
11 +app.use(errorController.pageNotFoundError);
12 +
13 +
14 +var server = http.listen(port, function(){
15 + console.log(`http://localhost:${port}`);
16 +})
17 +
1 +key=RwxSWXH88b2bKOAT6Ot3FHorPZQW9omma0xYIjtJe0JIKe4DC7TjX7Uj6E1ArzYi2AvVETmPrAIYyY8FlL%2BfAA%3D%3D
...\ No newline at end of file ...\ No newline at end of file
1 +let request = require('request');
2 +let cheerio = require('cheerio');
3 +const bus_url = 'http://apis.data.go.kr/6410000/busarrivalservice/getBusArrivalList';
4 +//const stationID = '228000708'; // 사색의광장 들어오는 방향
5 +const gateStationID = '203000125'
6 +require('dotenv').config();
7 +//const BusArrivalUrl = bus_url + '?servicekey=' + process.env.key + '&stationId=' + stationID;// 사색의광장 정류장 버스 도착 정보 조회용
8 +//console.log(BusArrivalUrl);
9 +var routeID = ['200000112', '200000115', '234000016', '200000103'];
10 +var Bus = [];
11 +// request(BusArrivalUrl, (err, res, body) => {
12 +// var $ = cheerio.load(body, {decodeEntities: false});
13 +// $('busArrivalList').each(function(idx){
14 +// let route = $(this).find('routeId').text();
15 +// routeID.push(route);
16 +// })
17 +// //console.log(routeID);
18 +// })
19 +const route_url = 'http://apis.data.go.kr/6410000/busrouteservice/getBusRouteInfoItem';
20 +var index = 0;
21 +function getBusNum(){
22 + var BusRouteUrl = route_url + '?servicekey=' + process.env.key + '&routeId='; // 각 버스 정보 조회용
23 + BusRouteUrl += routeID[index++];
24 + //console.log(BusRouteUrl);
25 + request(BusRouteUrl, (err, res, body) => {
26 + var $ = cheerio.load(body, {decodeEntities: false});
27 + $('busRouteInfoItem').each(function(idx){
28 + var id = $(this).find('routeId').text(); //버스 노선 id
29 + var num = $(this).find('routeName').text(); //버스 번호
30 + var firsttime = $(this).find('upFirstTime').text(); //평일 기점 첫차시간
31 + var lasttime = $(this).find('upLastTime').text(); //평일 기점 막차 시간
32 + var mintime = $(this).find('peekAlloc').text(); //평일 최소 배차시간
33 + var maxtime = $(this).find('nPeekAlloc').text(); //평일 최대 배차시간
34 + //var idx = Bus.findIndex((item, idx) => { return item.routeId = id})
35 + var newBus = new Object();
36 + newBus.routeId = id;
37 + newBus.BusNum = num;
38 + newBus.FirstTime = firsttime;
39 + newBus.LastTime = lasttime;
40 + newBus.MinTime = mintime;
41 + newBus.MaxTime = maxtime;
42 + console.log(newBus);
43 + Bus.push(newBus);
44 + console.log(Bus);
45 + })
46 + })
47 +}
48 +for(var i=0; i<routeID.length; i++){
49 + getBusNum();
50 +}
51 +const GateBusUrl = bus_url + '?servicekey=' + process.env.key + '&stationId=' + gateStationID; //국제캠 정문 정류장
52 +let date = new Date();
53 +let predictTime = ['-1', '-1', '-1', '-1'];
54 +let body = new Object();
55 +function predict(){
56 + console.log(GateBusUrl);
57 + request(GateBusUrl, (err, res, body) => {
58 + var $ = cheerio.load(body, {decodeEntities: false});
59 + $('busArrivalList').each(function(idx){
60 + let route = $(this).find('routeId').text();
61 + var index = Bus.findIndex(function(e, idx){ return e.routeId == route;});
62 + if(index > -1){
63 + //var num = $(this).find('plateNo1').text(); // 버스 차량번호 ex) 70사 1290
64 + var time = $(this).find('predictTime1').text(); // 버스 예상 도착시간
65 + var predictHour1 = date.getHours();
66 + var predictMinute1 = date.getMinutes() + Bus[index].mintime - (3 - (time*1)); // 사색의 광장 -> 경희대학교 3분정도 소요
67 + var predictHour2 = date.getHours();
68 + var predictMinute2 = date.getMinutes() + Bus[index].maxtime - (3 - (time*1)); // time*1 : string to integer 형변환
69 + if(predictMinute1 >= 60){
70 + predictHour1 += 1;
71 + predictMinute1 -= 60;
72 + }
73 + if(predictMinute2 >= 60){
74 + predictHour2 += 1;
75 + predictMinute2 -= 60;
76 + }
77 + Bus[index]["MinPredictTime"] = predictHour1 + ":" + predictMinute1;
78 + Bus[index]["MaxPredictTime"] = predictHour2 + ":" + predictMinute2;
79 + predictTime = [predictHour1 + "", predictMinute1 + "", predictHour2 + "", predictMinute2 + ""]
80 + const dateBusDeparture = new Date(date.getFullYear(), date.getMonth(), date.getDate(), predictTime[0]*1, predictTime[1]*1, 0);
81 + var gapSec = (dateBusDeparture.getTime() - date.getTime())/1000;
82 + var gapHour = Math.floor(gapSec / 60 / 60);
83 + var gapMin = Math.floor((gapSec - gapHour * 3600) / 60);
84 + // var ans = gapHour + "시간" + gapMin + "분 이상";
85 + // var ETD_min_H = predictTime[0];
86 + // var ETD_min_M = predictTime[1];
87 + // var ETD_max_H = predictTime[2];
88 + // var ETD_max_M = predictTime[3];
89 +
90 +
91 + //controller에 데이터 전송
92 + module.exports.data = {};
93 + module.exports.data.remainTime = gapHour + "시간" + gapMin + "분 이상";
94 + module.exports.data.ETD_min_H = predictTime[0];
95 + module.exports.data.ETD_min_M = predictTime[1];
96 + module.exports.data.ETD_max_H = predictTime[2];
97 + module.exports.data.ETD_max_M = predictTime[3];
98 +
99 +
100 + }
101 + })
102 + })
103 +}
104 +
105 +
106 +
107 +function start(){
108 + setInterval(predict, 60000);
109 +}
110 +setTimeout(start, 20000);
This diff is collapsed. Click to expand it.
1 { 1 {
2 - "name": "bustime", 2 + "name": "busterm",
3 "version": "1.0.0", 3 "version": "1.0.0",
4 "description": "", 4 "description": "",
5 - "main": "BusInfo.js", 5 + "main": "app.js",
6 "scripts": { 6 "scripts": {
7 "test": "echo \"Error: no test specified\" && exit 1" 7 "test": "echo \"Error: no test specified\" && exit 1"
8 }, 8 },
9 - "repository": {
10 - "type": "git",
11 - "url": "http://khuhub.khu.ac.kr/2020105677/BusTime.git"
12 - },
13 "author": "", 9 "author": "",
14 "license": "ISC", 10 "license": "ISC",
15 "dependencies": { 11 "dependencies": {
16 - "cheerio": "^1.0.0-rc.9", 12 + "dotenv": "^10.0.0",
17 - "require": "^2.4.20" 13 + "ejs": "^3.1.6",
14 + "express": "^4.17.1",
15 + "http-status-codes": "^2.1.4"
18 } 16 }
19 } 17 }
......
1 +var express = require('express');
2 +var app = express();
3 +var fs = require('fs')
4 +var Businfo = require("../models/BusInfo");
5 +
6 +app.set('views',__dirname+'/views');
7 +app.set('view engine', 'ejs');
8 +//app.engine('html',require('ejs').renderFile);
9 +app.engine('ejs', require('ejs').renderFile);
10 +
11 +exports.mainView = function(req, res) {
12 + res.render("index.ejs")
13 +}//main 화면
14 +
15 +exports.timeTable = function(req, res) {
16 + res.render("ejstest.ejs",{busNum:req.query.busNum, remainTime: Businfo.data.remainTime, ETD_min_H: Businfo.data.ETD_min_H,
17 + ETD_min_m: Businfo.data.ETD_max_m, ETD_max_H: Businfo.data.ETD_max_H, ETD_max_m: Businfo.data.ETD_max_m });
18 +} //timeTable 화면 ejs 변수 렌더링
19 +
20 +
1 +const httpStatus = require('http-status-codes');
2 +
3 +exports.pageNotFoundError = (req, res) => {
4 + let errorCode = httpStatus.NOT_FOUND;
5 + res.status(errorCode);
6 + res.send(`<h1>${errorCode}</h1> \n 찾는 페이지가 존재하지 않습니다. `);
7 +}
8 +
1 +const router = require("express").Router();
2 +const controller = require("./controller");
3 +
4 +router.get("/", controller.mainView); //main 페이지
5 +router.get("/timetable", controller.timeTable); //timetable 페이지
6 +
7 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
1 +<!doctype html>
2 +<html>
3 +<head>
4 + <title>Time Table</title>
5 + <meta charset="utf-8">
6 + <style type="text/css">
7 + a { text-decoration:none }
8 + </style>
9 +</head>
10 +<body>
11 + <h1><a href="/"><p style="text-align:center;">BTT</p></a></h1>
12 + <br> <div style="padding:0 0 0 20px;">
13 + <h2> <%= busNum %> BUS Time Table</h2>
14 + </div>
15 + <div style="padding:0 0 0 20px;">
16 + <table border="3" width="400">
17 + <th> Index</th>
18 + <th> 출발 예정시간</th>
19 + <th> 남은 시간</th>
20 + <tr align="center">
21 + <td> text입력하기 </td>
22 + <td> <%= ETD_min_H %>:<%= ETD_min_m %> ~ <%= ETD_max_H %>:<%= ETD_max_m %> </td>
23 + <td> <%= remainTime %> </td>
24 + </tr>
25 + </table>
26 + </div>
27 + <br>
28 + <br>
29 + <p> <div style="padding:0 0 0 20px;">
30 + 버스의 평균 배차시간을 기준으로 최소 출발시간과 최대 출발시간을 제공합니다.
31 + </div> </p>
32 + <div style="padding:0 0 0 20px;">
33 + <% var date = new Date(); %>
34 + <% var yyyy = date.getFullYear(); %>
35 + <% var mm1 = date.getMonth() + 1; %>
36 + <% var dd = date.getDate(); %>
37 + <% var hh = date.getHours(); %>
38 + <% var mm2 = date.getMinutes(); %>
39 + <% var printDateNotion = yyyy+"년 "+mm1+"월 "+dd+"일 "; %>
40 + <% var printDateNotion2 = hh+"시 "+mm2+"분 기준"; %>
41 +
42 + <%= printDateNotion %>
43 + <%= printDateNotion2 %>
44 + </div>
45 +</body>
46 +</html>
...\ No newline at end of file ...\ No newline at end of file
1 +<!doctype html>
2 +<html>
3 +<head>
4 + <title>BUS TT</title>
5 + <meta charset="utf-8">
6 + <style type="text/css">
7 + a { text-decoration:none }
8 + </style>
9 +</head>
10 +<body>
11 + <h1><p style="text-align:center;">BTT</p></h1>
12 + <br>
13 + <link rel='stylesheet' type='text/css' href='/css/style.css' />
14 + <h2 style="padding:0 0 0 20px;"> 사색의 광장 Bus Time Table 조회 서비스에 오신 것을 환영합니다.</h2>
15 + <p style="padding:0 0 0 20px;"> 이곳에는 사색의 광장에서 출발하는 모든 버스의 정보가 있습니다.</p>
16 + <br><br>
17 + <h2 style="padding:0 0 0 20px;"> 버스별 시간표 조회 (click the bus number!)</h2>
18 +
19 + <div style="padding:0 0 0 20px;">
20 + <% var date = new Date(); %>
21 + <% var yyyy = date.getFullYear(); %>
22 + <% var mm1 = date.getMonth() + 1; %>
23 + <% var dd = date.getDate(); %>
24 + <% var hh = date.getHours(); %>
25 + <% var mm2 = date.getMinutes(); %>
26 + <% var printDateNotion = yyyy+"년 "+mm1+"월 "+dd+"일 오늘 운행하는 버스들 "; %>
27 + <% var printDateNotion2 = "( "+hh+"시 "+mm2+"분 기준 )"; %>
28 +
29 + <%= printDateNotion %>
30 + <%= printDateNotion2 %>
31 + </div>
32 + <ul>
33 + <li><a href="http://localhost:23023/timetable?busNum=9">9</a></li>
34 + <li><a href="http://localhost:23023/timetable?busNum=1112">1112</a></li>
35 + <li><a href="http://localhost:23023/timetable?busNum=5100">5100</a></li>
36 + <li><a href="http://localhost:23023/timetable?busNum=7000">7000</a></li>
37 + </ul>
38 + <p> ejs파일임</p>
39 +</body>
40 +</html
...\ No newline at end of file ...\ No newline at end of file