forked from haystack/nb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (57 loc) · 2.12 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
55
56
57
58
59
60
61
62
63
64
65
66
67
const express = require('express');
const passport = require('passport');
const path = require('path');
const logger = require('morgan');
const cors = require('cors');
const Source = require('./models').Source;
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const classesRouter = require('./routes/classes');
const filesRouter = require('./routes/files');
const annotationsRouter = require('./routes/annotations');
const spotlightsRouter = require('./routes/spotlights');
const gradesRouter = require('./routes/grades');
require('./auth/auth');
const app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, '/dist')));
app.use(cors({
origin: function(origin, callback){
if (
!origin
|| origin == 'https://nb2.csail.mit.edu'
|| origin == 'https://127.0.0.1:8080'
|| origin == 'https://jumana-nb.csail.mit.edu'
|| origin == 'https://akshahk-nb.csail.mit.edu'
|| origin == 'https://localhost:8080'
){
callback(null, true);
}
else{
Source.findOne({where: {origin: origin}}).then((source) => {
if (source){
callback(null, true);
} else {
console.log(origin);
callback( new Error('Not allowed by CORS'));
}
});
}
},
credentials: true}));
app.use('/', express.static('public'));
app.use('/', indexRouter);
app.use('/api/users', usersRouter);
app.use('/api/classes', passport.authenticate('jwt', { session: false }), classesRouter);
app.use('/api/files', passport.authenticate('jwt', { session: false }), filesRouter);
app.use('/api/annotations', passport.authenticate('jwt', { session: false }), annotationsRouter);
app.use('/api/spotlights', passport.authenticate('jwt', { session: false }), spotlightsRouter);
app.use('/api/grades', passport.authenticate('jwt', { session: false }), gradesRouter);
// Handle errors.
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json({ success: false, error: err });
});
module.exports = app;