-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
54 lines (45 loc) · 1.57 KB
/
app.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
//import 3rd party libraries
const express = require('express');
const bodyParser = require('body-parser');
const helmet = require('helmet');
//import the routes
const studentRoutes = require('./routes/student');
const courseRoutes = require('./routes/course');
const authRoutes = require('./routes/auth');
const adminRoutes = require('./routes/admin');
const alertRoutes = require('./routes/alert');
const app = express();
// const pw = '1234';
// bcrypt.hash(pw,12).then(hashedpw => {
// console.log(hashedpw);
// })
// .catch(err => {
// console.log(err);
// });
//use helmet to add secure headers to node application
app.use(helmet());
//use body parser to parse json data
app.use(bodyParser.json());
//set headers to handle CORS(Cross-Origin Resource Sharing) errors
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
//middleware to handle routes
app.use('/api/student', studentRoutes);
app.use('/api/course', courseRoutes);
app.use('/api/auth', authRoutes);
app.use('/api/admin', adminRoutes);
app.use('/api/alert', alertRoutes);
//middleware to handle errors
app.use((error, req, res, next) => {
console.log(error);
const status = error.statusCode || 500;
const message = error.message;
const data = error.data;
res.status(status).json({message: message, data: data});
});
//start the server on port 3030
app.listen(process.env.PORT || 3030);