This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from BusHanyang/update-api
Update logic of api
- Loading branch information
Showing
10 changed files
with
157 additions
and
471 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var app = require("../index") | ||
var http = require("http"); | ||
|
||
const PORT = 8080; | ||
|
||
var server = http.createServer(app); | ||
|
||
server.listen(PORT); | ||
|
||
server.on("error", err => { | ||
if (error.syscall !== "listen") { | ||
throw error; | ||
} | ||
|
||
switch (error.code) { | ||
case "EACCES": | ||
console.error(PORT + " requires elevated privileges"); | ||
process.exit(1); | ||
break; | ||
case "EADDRINUSE": | ||
console.error(PORT + " is already in use"); | ||
process.exit(1); | ||
break; | ||
default: | ||
throw error; | ||
} | ||
}); | ||
server.on("listening", () => { | ||
console.log("Listening on " + PORT); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
const util = require("../common"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
/* router */ | ||
module.exports = function(app) { | ||
/* full path */ | ||
app.use("/:daykind/:isWeek/:where", Validation); | ||
|
||
/* short path */ | ||
app.use("/:where", halt, Validation); | ||
|
||
/* error handle */ | ||
app.use("*", (req, res, next) => { | ||
next({ errorcode: "404" }); | ||
}); | ||
|
||
app.use((err, req, res, next) => { | ||
console.log(err); | ||
res.status(404); | ||
res.json(err); | ||
}); | ||
}; | ||
|
||
function halt(req, res, next) { | ||
let isHalt = util.isHalt(); | ||
if (isHalt == "halted") { | ||
res.status(200).json({ status: "Halt" }); | ||
return false; | ||
} else { | ||
next(); | ||
} | ||
} | ||
|
||
function Validation(req, res, next) { | ||
let { | ||
daykind = util.getDateKind(), | ||
isWeek = util.getDayKind(), | ||
where | ||
} = req.params; | ||
|
||
if ( | ||
(daykind == "semester" || | ||
daykind == "vacation" || | ||
daykind == "vacation_session") && | ||
(isWeek == "week" || isWeek == "weekend") | ||
) { | ||
sendResult(daykind, isWeek, where, res, next); | ||
} else { | ||
next({ error: "Incorrect column1" }); | ||
} | ||
} | ||
|
||
function sendResult(daykind, isWeek, where, res, next) { | ||
let pre; | ||
if (where == "giksa") { | ||
pre = "Residence_"; | ||
} else if (where == "shuttlecock_i") { | ||
pre = "Shuttlecock_I_"; | ||
} else if (where == "shuttlecock_o") { | ||
pre = "Shuttlecock_O_"; | ||
} else if (where == "subway") { | ||
pre = "Subway_"; | ||
} else if (where == "yesulin") { | ||
pre = "YesulIn_"; | ||
} else { | ||
next({ error: "Incorrect column2" }); | ||
} | ||
|
||
fs.readFile( | ||
path.join( | ||
__dirname, | ||
"../timetable", | ||
daykind, | ||
isWeek, | ||
pre + isWeek + ".json" | ||
), | ||
(err, data) => { | ||
if (err) next({ error: "Incorrect column3" }); | ||
try { | ||
data = JSON.parse(data); | ||
res.json(data[pre.toLowerCase() + isWeek]); | ||
} catch (err) { | ||
next({ error: "Incorrect column4" }); | ||
} | ||
} | ||
); | ||
} |
Oops, something went wrong.