forked from SinghDivyajeet/ipriksha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
66 lines (56 loc) · 1.7 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
var express = require('express');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var expressSession = require('express-session');
var mongoose = require('mongoose');
var path = require('path');
var passport = require('passport');
var localStrategy = require('passport-local').Strategy;
var app = express();
var routes = require('./routes/api.js');
// User schema
var User = require('./models/User.js');
if (process.env.MONGODB_URI)
mongoose.connect(process.env.MONGODB_URI);
else
console.log("Error: MONGODB_URI is undefined!")
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('express-session')({
secret: process.env.EXPRESS_SESSION_SECRET,
resave: false,
saveUninitialized: false
}));
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(passport.initialize());
app.use(passport.session());
// Create link to Angular build directory
var distDir = __dirname + "/dist/frontend/";
app.use(express.static(distDir));
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();
});
// Routes
app.use('/user/', routes);
// Error handlers
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use(function(err, req, res) {
res.status(err.status || 500);
res.end(JSON.stringify({
message: err.message,
error: {}
}));
});
module.exports = app;