forked from amanscisingh/lmsTeacherEnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (133 loc) · 4.27 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
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
152
153
154
155
156
157
158
159
160
const express = require('express');
const mongoose = require('mongoose');
var bodyParser = require('body-parser')
const exphbs = require('express-handlebars');
const cookieParser = require('cookie-parser');
const path = require('path');
const cors = require('cors');
const moment = require('moment');
// imports for the file upload feature
const crypto = require('crypto');
const multer = require('multer');
const { GridFsStorage } = require('multer-gridfs-storage');
const Grid = require('gridfs-stream');
const methodOverride = require('method-override');
const app = express();
// using body parser
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json());
// using dotenv
require('dotenv').config({ path: './config/.env' })
const URL = process.env.MONGODB_URI.toString();
const PORT = process.env.PORT;
// setting templating engineapp.engine('.hbs', exphbs({helpers:{ formatDate, indexing }, defaultLayout: 'main', extname: '.hbs'}));
function parseToString(input) {
return input.toString();
};
// helper functions
function formatDate(date, format) {
return moment(date).format(format)
};
function formatPassword(password) {
if (password.length === 0) {
return 'NA';
} else {
return password
}
}
app.engine('.hbs', exphbs({helpers:{ parseToString, formatDate, formatPassword }, defaultLayout: 'main', extname: '.hbs'}));
app.set('view engine', '.hbs');
// setting up public folder
const __dirname__ = path.resolve();
app.use(express.static(path.join(__dirname__, 'public')));
// connecting to db and starting the server
mongoose.connect(URL, { useNewUrlParser: true, useUnifiedTopology: true}).then(() => {
app.listen(PORT, () => {
console.log(`Server started on port ${PORT} and DB connected as well!!!`);
})
});
// for file UPLOAD
const con = mongoose.createConnection(URL, {
useUnifiedTopology: true,
useNewUrlParser: true
});
con.then( () => {
console.log(`Files upload DB is connected as well!!!`)
})
//INIT gfs
let gfs;
con.once('open', () => {
//initialize stream
gfs = Grid(con.db, mongoose.mongo);
gfs.collection('uploads');
});
//create storage engine
const storage = new GridFsStorage({
url: URL,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
const filename = buf.toString('hex') + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'uploads'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
app.use(cors());
app.use(methodOverride('_method'));
// using cokie parser
app.use(cookieParser());
// routes
app.use('/', require('./routes/index'));
app.use('/auth', require('./routes/auth'));
app.use('/teacherDashboard', require('./routes/teacherDashboard'));
app.use('/api', require('./routes/api'));
// app.use('/upload', upload.single('file'), require('./routes/upload'));
app.post('/upload', upload.single('file'), (req, res) => {
try {
console.log('post request received');
res.send({ file: req.file });
} catch (error) {
res.send(error);
}
})
app.get('/files', (req, res) => {
gfs.files.find().toArray((err, files) => {
// Check if files
if (!files || files.length === 0) {
return res.status(404).json({
err: 'No files exist'
});
}
// Files exist
return res.json(files);
});
});
app.get('/open/:filename', (req, res) => {
gfs.files.findOne({ filename: req.params.filename }, (err, file) => {
// Check if file
if (!file || file.length === 0) {
return res.status(404).json({
err: 'No file exists'
});
}
// // Check if image
if (file.contentType === 'image/jpeg' || file.contentType === 'image/png' || file.contentType === 'application/pdf' || file.contentType === 'application/octet-stream' || file.contentType === 'text/plain') {
// Read output to browser
const readstream = gfs.createReadStream(file);
readstream.pipe(res);
} else {
res.status(404).json({
err: 'Not an image'
});
}
});
});