-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
129 lines (101 loc) · 3.27 KB
/
server.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
// Description: This file is the entry point to our application
// Importing dependencies
const express = require('express');
const passport = require('passport');
const mongoose = require('mongoose');
const cors = require('cors');
const bodyParser = require('body-parser');
const flash = require('express-flash');
const session = require('express-session');
const getUserByEmail = require('./helpers/getUserByEmail');
const getUserById = require('./helpers/getUserById');
const initilizePassport = require('./config/passport.config');
// initilizing passportjs
initilizePassport(passport, getUserByEmail, getUserById);
// Initializing variables
require("dotenv").config();
const app = express();
const PORT = process.env.PORT || 5000;
const connectDB = require('./config/connectDB');
const registerUser = require('./controllers/register');
// Importing routes
//const mainRoute = require('./routes/mainRoute');
const ticket = require('./routes/ticket');
const voyage = require('./routes/voyage');
app.use(bodyParser.json());
app.use(flash());
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//Routes
app.use("/ticket", ticket);
app.use("/voyage", voyage);
app.post("/login",passport.authenticate('local',{
failureFlash: true
}), (req,res) => {
try{
res.status(200).send("Authenticated successfully");
} catch (error) {
res.status(500).send(error);
}
})
app.post('/register', async (req, res) => {
registerUser(req, res);
});
app.delete("/logout", (req, res) => {
req.logOut(err=>console.log(err));
res.status(200).send("Logged out successfully");
});
// Used for testing purposes remove comment if you wish to add a train or a voyage to the database
// The sync route is used in case of any changes to the database schema (reservation)
// const Train = require('./models/Train');
// const Reservation = require('./models/Reservation');
// app.post("/addTrain", async (req, res) => {
// try {
// const { name, seats, from, to, price } = req.body;
// const train = new Train({
// name: name,
// seats: seats,
// from: from,
// to: to,
// price: price,
// });
// await train.save();
// res.status(200).send("Train added successfully");
// } catch (error) {
// res.status(500).send(error);
// }
// });
// app.post("/sync", async (req, res) => {
// Reservation.syncIndexes();
// res.status(200).send("Synced successfully");
// })
// const Voyage = require('./models/Voyage');
// app.post("/addVoyage", async (req, res) => {
// const { train, departure, arrival, from, to } = req.body;
// const currentTrain = await Train.findOne({ _id: train });
// if (!currentTrain) {
// res.status(404).send("Train not found");
// }
// const voyage = new Voyage({
// train: train,
// departure: departure,
// arrival: arrival,
// from: from,
// to: to,
// remainingSeats: currentTrain.seats,
// });
// await voyage.save();
// res.status(200).send("Voyage added successfully");
// });
app.listen(PORT, () => {
connectDB(process.env.MONGO_URI)
console.log(`Server is running on port ${PORT}`);
});