-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·83 lines (74 loc) · 2.28 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
const express = require('express');
const app = express();
const File = require('./models/file');
const connectDB = require('./helpers/db');
const cors = require('cors')
const { filesRoute } = require('./routes/files'); // Import the router
const { userverify } = require('./middleware/userMiddleware');
const cookieparser = require('cookie-parser');
const { router } = require('./routes/authRoutes'); // Import the router
const { authverify } = require('./middleware/authMiddleware');
const Fuse = require('fuse.js');
// Connect to the database
connectDB();
// Middleware to parse JSON bodies
app.use(express.json());
app.use(cors());
app.use(express.urlencoded({ extended: true }));
app.use('/static', express.static('static'))
app.use('/static', express.static('uploads'))
app.use(cookieparser())
app.set('view engine', 'ejs');
// Use the files route
app.use(router);
// app.user(userverify)
app.get("*",userverify)
app.use('/api/files', filesRoute);
app.get('/', userverify,(req, res) => {
res.render('home')
})
app.get('/login', (req, res) => {
res.render('login')
})
app.get('/signup', (req, res) => {
res.render('signup')
})
app.get('/upload', authverify,(req, res) => {
res.render('upload')
})
app.get('/uploads/:id',authverify, (req, res) => {
res.download(__dirname+"/uploads/"+req.params['id'])
})
// function sleep(ms) {
// return new Promise((resolve) => {
// setTimeout(resolve, ms);
// });
// }
// app.get('/')
app.get('/browse', async (req, res) => {
var query = req.query.q ?? "";
var allResult = await File.find({});
var result=[]
console.log(query);
if (query) {
const fuse = new Fuse(allResult,{keys:['subject','courseCode','examType','year','sem']})
var returnedResult = fuse.search(query);
returnedResult.forEach(element => {
result.push(element.item)
});
} else {
result=allResult
}
// await sleep(10000)
console.log(result)
// res.end()
res.render('browse', { result })
})
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
console.log(`http://localhost:${PORT}`);
// setTimeout(async () => {
// console.log(await File.find({}))
// }, 5000);
});