-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
43 lines (38 loc) · 1.06 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
var express = require('express');
var app = express();
var expressJwt = require('express-jwt');
var bodyParser = require('body-parser');
var UserModel = require('./models/users.model');
var config = require('./config');
require('./lib/db.js');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app
.use(expressJwt({ secret: config.jwt.secret, requestProperty: 'payload' })
.unless({
path: [
{ url: '/api/token', methods: ['POST'] },
{ url: '/api/user', methods: ['POST'] },
],
}));
app.use(function(req, res, next) {
// For un-authenticated requests
if (!req.payload) {
return next();
}
var userid = req.payload.sub;
UserModel.getById(userid, function(err, users) {
if (err || !users.length) {
return res.sendStatus(401);
}
req.user = users[0];
next();
});
});
// Setup Routes
require('./router')(app);
// Initialize app
var server = app.listen(process.env.PORT || 3030, function() {
var port = server.address().port;
console.log("Meri class App is now running on port", port);
});