-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
executable file
·33 lines (31 loc) · 1.03 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
var express = require('express'),
app = express(),
port = process.env.PORT || 3000,
mongoose = require('mongoose'),
User = require('./api/models/userModel'),
bodyParser = require('body-parser');
jsonwebtoken = require("jsonwebtoken");
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://school:school@ds249079.mlab.com:49079/schoolcom');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//Middleware for user validation start...
app.use(function(req, res, next){
if(req.headers && req.headers.authorization && req.headers.authorization.split(' ')[0]=='JWT'){
jsonwebtoken.verify(req.headers.authorization.split(' ')[1], 'schoolcom',function(err, decode){
if(err){
req.user = undefined;
}
req.user = decode;
next();
});
} else {
req.user = undefined;
next();
};
});
//Middleware for user validation end.....
var routes = require('./api/routes/schoolcomRoutes');
routes(app);
app.listen(port);
console.log('SchoolCom API server started on: ' + port);