-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
86 lines (68 loc) · 1.98 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const express = require('express'),
cors = require('cors');
const logger = require('morgan');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const fs = require('fs');
const path = require('path');
const jsdom = require("jsdom");
const $ = require('jquery');
const http = require('http');
const passport = require('passport');
const config = require('./config/database');
// Connect to database
mongoose.Promise = global.Promise;
mongoose.connect(config.database);
// On Connection
mongoose.connection.on('connected', () => {
console.log('Connected to database.. .');
});
// On Error
mongoose.connection.on('error', (err) => {
console.log('DATABASE ERROR: ' + err);
});
// Initializing our app at express
const app = express();
app.use(helmet());
// Routes_variable
const modules = require('./routes/modules');
const users = require('./routes/users');
// Middlewares
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport);
// Routes
app.use(express.static('public'));
app.use('/modules', cors(), modules);
app.use('/users', cors(), users);
// Catch 404 Errors and forward them to error handler
app.use((req, res, next) => {
const err = new Error('Not Found!');
err.status = 404;
next(err);
});
// Error handler function
app.use((err, req, res, next) => {
const error = app.get('env') === 'development' ? err : {};
const status = err.status || 500;
// Respond to client
res.status(status).json({
error: {
message: error.message
}
});
//Respond to ourselves
console.error(err);
});
// Read Directory
var files = fs.readdir("./", (err, files) => {
if(err)
console.log(err);
console.log("files: " + files.join(" | "));
})
// Start the server
const port = app.get('port') || 8000;
app.listen(port, () => console.log(`Server is listening on port ${port}`));