-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
82 lines (65 loc) · 2.36 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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const bcrypt = require('bcryptjs');
const fs = require('fs');
//connect-multiparty OU multer
const multipart = require('connect-multiparty');
const multipartMiddleware = multipart({ uploadDir: './public' });
const mongoose = require('./configdb/db');
const userController = require('./controllers/userController');
const instructorController = require('./controllers/instructorController');
const classroomController = require('./controllers/classroomController');
const RequestsClassroomController = require('./controllers/requestsClassroomController');
const adminController = require('./controllers/adminController');
const Admin = require('./models/admin');
const app = express();
//make public DIR accessible
app.use(express.static('public'));
//Accept Files
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
app.use('/user', userController);
app.use('/instructor', instructorController);
app.use('/classroom', classroomController);
app.use('/requestsClassroom', RequestsClassroomController);
app.use('/admin', adminController);
app.get('/', function (req, res) {
res.send("Welcome to the server !");
})
app.get('/testChartJsData', (req, res) => {
let years = ['Ali', 'Fathi', 'Mouldi', 'Mohamed'];
let data = [
{ data: [4, 16, 15, 34], label: 'Series A' }
];
res.status(200).send({ years, data })
});
app.post('/testFileAndData', multipartMiddleware, (req, res) => {
if (req.files) {
let path = req.files.image.path;
let ext = path.substr(path.indexOf('.'));
let newName = "abc";
fs.renameSync(path, "public/" + newName + ext);
}
res.status(200).send('OK');
})
const port = 3000 || process.env.PORT
app.listen(port, async () => {
try {
let admins = await Admin.find();
if (admins.length == 0) {
let salt = bcrypt.genSaltSync(10);
let hashedPassword = bcrypt.hashSync("123456789", salt);
let admin = new Admin({
email: "[email protected]",
password: hashedPassword
})
let savedAdmin = await admin.save();
console.log("Admin Added");
}
console.log("Server Started");
} catch (error) {
console.log(error);
}
});