-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
79 lines (65 loc) · 2.06 KB
/
server.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
const PROJECT_NAME = 'todoloo'
// x..x..x..x..x..x..x..x..x..x..x..x..x..x..x..x..x..x..x..x..x..x
const fs = require('fs');
const bodyParser = require('body-parser');
const express = require('express');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const passport = require('passport');
const renderFile = require('ejs').renderFile
// Load Configuration
const appMiddleWare = require('./config/middleware.js')
const appSecrets = require('./config/secrets.js')
const appAuthentication = require('./config/auth.js')
const connectToDB = require('./config/db-setup.js').connectToDB
// Import Routers
let indexRouter = require('./routes/indexRouter.js')
let authRouter = require('./routes/authRouter.js')
// Load DB User Model (for appAuthentication configuration)
let User = require('./db/schemas/userSchema.js').User
// =========
// RUN APP
// =========
const app = express()
const PORT = process.env.PORT || 3000
app.set('port', PORT)
// =========
// VIEW ENGINE
// =========
app.set('views', './dist/views');
app.engine('html', renderFile)
app.set('view engine', 'html');
// =========
// DATABASE
// =========
connectToDB(PROJECT_NAME)
// =========
// APPLICATION MIDDLEWARE
// =========
app.use( express.static( __dirname + '/dist/assets') );
app.use( bodyParser.json() );
app.use( bodyParser.urlencoded() );
app.use( cookieParser() );
app.use( session({secret: appSecrets.sessionSecret }) );
app.use( passport.initialize() );
app.use( passport.session() );
appAuthentication(User)
app.use( appMiddleWare.cookifyUser )
app.use( appMiddleWare.parseQuery )
//
// =========
// ROUTERS
// =========
app.use( '/', indexRouter )
app.use( '/auth', authRouter )
//add all apiRouters as /api middleware
let apiDir = __dirname + '/routes/api'
let files = fs.readdirSync(apiDir)
var routers = files.map(file => require(`${apiDir}/${file}`) );
routers.forEach(function(router) {
app.use ( '/api', router )
})
app.use(appMiddleWare.errorHandler);
app.listen(PORT,function() {
console.log('\n\n===== listening for requests on port ' + PORT + ' =====\n\n')
})