-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
48 lines (43 loc) · 1.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import express from "express";
import axios from "axios";
import bodyParser from "body-parser";
const app = express();
const port = 3000;
const APP_URL = "https://api.railwayapi.site";
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.get("/", (req, res) => {
res.render("index.ejs", {
content: "Ensure your train number consists of 5 digits (e.g., 12601).",
});
});
app.get("/what.ejs", (req, res) => {
res.render("what.ejs");
});
app.get("/how.ejs", (req, res) => {
res.render("how.ejs");
});
app.get("/who.ejs", (req, res) => {
res.render("who.ejs");
});
app.post("/search", async (req, res) => {
const trainNumber = req.body.trainNumber;
if (trainNumber.length != 5) {
res.render("index.ejs", {
content: "Please enter a 5 digit train number!",
});
}
try {
const response = await axios.get(APP_URL + `/api/v1/trains/${trainNumber}`);
const trainInfo = response.data.data[0];
res.render("index.ejs", { trainInfo });
} catch (error) {
console.log(`error: ${error}`);
res.render("index.ejs", {
error: "Error fetching train information. Please try again later.",
});
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});