-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
71 lines (61 loc) · 2.57 KB
/
app.js
File metadata and controls
71 lines (61 loc) · 2.57 KB
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
const express = require('express');
const connectDB = require('./db/connect');
const mongoose = require('mongoose');
const LoginRoute = require('./operationHandling/loginroute');
const signupRoute = require('./operationHandling/singuproute');
const bookingRoute = require('./operationHandling/bookingroute');
const app = express();
require('dotenv').config(); // for env files
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); // To parse data in the body
app.set("view engine", "ejs"); // using template engine
mongoose.set('strictQuery', true); // To avoid Deprecation WARNINGS
app.use(express.static('./public'));
app.use(express.json());
const { storage } = require('./file-controller');
const { ref, listAll, getDownloadURL } = require('firebase/storage');
// const UserRoutes = require('./Routes/UserRoute')
// app.use('/', UserRoutes)
// const RentRoutes = require('./Routes/RentRoute');
// app.use('/Rent', RentRoutes)
// home page
app.get('/', async (req, res) => {
try {
const storageRef = ref(storage, ''); // reference to the root of storage
const result = await listAll(storageRef);
const imageUrls = await Promise.all(result.items.map((itemRef) => getDownloadURL(itemRef)));
const files = await Promise.all(result.items.map(async (itemRef) => {
const url = await getDownloadURL(itemRef);
const fullname = itemRef.name.substring(0, itemRef.name.lastIndexOf('.'));
const name = fullname.split(' ');
return { name: name[0], year: name[1], price: name[2], url };
}));
res.render('pages/index', { files });
} catch (error) {
console.error('Error retrieving images:', error);
res.status(500).send('Error retrieving images');
}
}); // rendering the page
app.post('/', function(req, res) {
bookingRoute(req,res);
}); // validating the form
// registration page
app.get('/registration', function(req, res) {
res.render('pages/registration');
}); // rendering the page
app.post('/registration', async (req,res) => {
signupRoute(req, res);
}) // validating the form
const port = 3000;
const start = async () => {
try {
// await client.db("Car-Rental").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
await connectDB(process.env.MONGO_URI);
app.listen(port, console.log(`Server is listening on ${port}`));
} catch (error) {
console.log(error);
}
}
start().catch(console.dir); // server starts