-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
77 lines (63 loc) · 1.57 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
// require express and other modules
var express = require('express'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
expressSession = require('express-session'),
mongoose = require('mongoose'),
hash = require('bcrypt-nodejs'),
path = require('path'),
ejs = require('ejs');
// create instance of express
var app = express();
// set templating engine
app.set('view engine', 'ejs');
// user schema/model
var User = require('./models').User;
// require routes
var api = require('./api'),
auth = require('./auth')
/**************
* MiddleWare *
**************/
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false })); // parse forms
app.use(cookieParser());
app.use(expressSession({
secret: 'so many questions',
resave: false,
saveUninitialized: false
}));
app.use(express.static(path.join(__dirname, 'public'))); // angular application
/**********
* Routes *
**********/
app.use(auth);
app.use(api);
app.get(["/", "*"], function(req, res){
res.send(
res.render('application.html.ejs', {user: req.user})
)
});
/**********
* Errors *
**********/
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: {}
}));
});
/**********
* Server *
**********/
app.listen(3000, function() {
console.log('server started on localhost:3000');
});