-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
102 lines (65 loc) · 2.93 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* jshint esversion:6, node: true */
/* Chingu Cohorts Build.To.Learn Project Registry API
© 2017 - ngChingu team
https://github.com/ngChingu/project_registry
*/
/* ================================= SETUP ================================= */
const express = require('express'),
app = express(),
morgan = require('morgan'),
bodyParser = require('body-parser'),
db = require('./db'),
mongoose = require('mongoose'),
passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
User = require('./models/user'),
Project = require('./models/project'),
apiRoutes = require('./routes/apiroutes'),
authRoutes = require('./routes/authroutes'),
publicRoutes = require('./routes/publicroutes'),
port = process.env.PORT || 3000;
/* ============================= CONFIGURATION ============================= */
// set static files location
app.use(express.static(__dirname + '/public'));
// enable logger
app.use(morgan('dev'));
// enable body parsing
app.use(bodyParser.urlencoded({ 'extended' : 'true' }));
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
/* ================================= CORS ================================= */
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
/* =============================== PASSPORT ================================ */
app.use(passport.initialize());
passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username }, { username: 1, salt: 1, hash: 1 }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false, { message: 'Incorrect User Name'}); }
if (!user.validatePassword(password)) {
return done(null, false, { message: 'Incorrect Password'});
}
return done(null, user);
});
}
));
/* ================================ ROUTES ================================= */
app.use(apiRoutes);
app.use(authRoutes);
app.use(publicRoutes);
/* ============================= ERROR HANDLER ============================ */
app.use(function (err, req, res, next) {
console.log('Error!\n', err.stack);
res.status(500).send('Something broke...');
});
/* ============================= CONNECT TO DB ============================= */
mongoose.connect(db.getDbConnectionString());
mongoose.Promise = global.Promise;
/* ================================ STARTUP ================================ */
app.listen(port, function () {
console.log(`Server listening on port ${port}.`);
});