-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
159 lines (133 loc) · 4.49 KB
/
app.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import express from "express";
import bodyParser from "body-parser";
import request from 'request-promise';
import path from "path";
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import connectToDatabase from "./connection.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
app.set('view engine', 'ejs');
app.set("views", path.join(__dirname, "veiws"));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('public'));
const port = process.env.PORT||5000
const logrequest= (req,res,next)=>{
console.log(`${new Date().toLocaleString()} Request made to ${req.originalUrl}`);
next();
}
app.listen(port, function () {
console.log(__dirname);
});
app.use(logrequest);
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
app.get("/about.html",function(req,res){
res.sendFile(__dirname + "/adout.html");
});
app.get("/contact.html",function(req,res){
res.sendFile(__dirname + "/contact.html");
});
app.post("/contactres", async function (req, res) {
var name = req.body.name;
var email = req.body.email;
var message = req.body.message;
const db = await connectToDatabase();
const collection = db.collection("contact");
collection.insertOne({ "Name": name, "Email": email, "Message": message });
//redirect to home page after successful submission?
res.redirect('/');
})
app.post("/find-train-between", async function (req, res) {
try {
let num1 = req.body.from;
let num2 = req.body.to;
let options = {
method: 'GET',
uri: `https://api.railwayapi.site/api/v1/trainsBtwStations?fromStation=${num1}&toStation=${num2}&allTrains=true`,
json: true,
};
const response = await request(options);
const trainDetails = response.data;
console.log(trainDetails)
res.render("train_between", { trainDetails });
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});
app.post("/trainno_search", async function (req, res) {
try {
let trainNumbers = req.body.pnr;
let options = {
method: 'GET',
uri: `https://api.railwayapi.site/api/v1/schedules/${trainNumbers}?fullSchedule=true`,
json: true,
};
const response1 = await request(options);
const trainData = {};
trainData['response1'] = response1.data[0];
options = {
method: 'GET',
uri: `https://api.railwayapi.site/api/v1/trains/${trainNumbers}`,
json: true,
};
const response2 = await request(options);
trainData['response2'] = response2.data[0];
console.log(trainData)
res.render("trainno", { trainData });
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});
app.get("/trainno_search", async function (req, res) {
try {
let trainNumbers = req.query.trainno;
let options = {
method: 'GET',
uri: `https://api.railwayapi.site/api/v1/schedules/${trainNumbers}?fullSchedule=true`,
json: true,
};
const response1 = await request(options);
const trainData = {};
trainData['response1'] = response1.data[0];
options = {
method: 'GET',
uri: `https://api.railwayapi.site/api/v1/trains/${trainNumbers}`,
json: true,
};
const response2 = await request(options);
trainData['response2'] = response2.data[0];
console.log(trainData)
res.render("trainno", { trainData });
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});
app.post("/pnr_search", async function (req, res) {
try {
let pnr = req.body.pnr;
console.log(pnr)
const options = {
method: 'GET',
url: 'https://indianrailways.p.rapidapi.com/index.php',
qs: pnr,
headers: {
'X-RapidAPI-Key': '518bb0fbcemshf77511bd22517bfp19a6f0jsn8b2573d18947',
'X-RapidAPI-Host': 'indianrailways.p.rapidapi.com'
}
};
const response = await request(options);
const pnrData = response.data;
console.log(pnrData)
res.send(pnrData)
// res.render("pnr", { pnrData });
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});