-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
79 lines (63 loc) · 2.16 KB
/
index.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
'use strict';
var express = require('express');
var kraken = require('kraken-js');
var basicAuth = require('express-basic-auth');
var cors = require('cors');
var expressJwt = require('express-jwt');
var utils=require('./utils');
var options, app;
// var mongo = require('mongodb');
/*
* Create and configure application. Also exports application instance for use by tests.
* See https://github.com/krakenjs/kraken-js#options for additional configuration options.
*/
options = {
onconfig: function (config, next) {
/*
* Add any additional config setup or overrides here. `config` is an initialized
* `confit` (https://github.com/krakenjs/confit/) configuration object.
*/
next(null, config);
}
};
app = module.exports = express();
app.use(kraken(options));
// var whitelist = ['http://localhost:4300', 'http://alfa.smartstorm.io']
// var corsOptions = {
// origin: function (origin, callback) {
// if (whitelist.indexOf(origin) !== -1) {
// callback(null, true)
// } else {
// callback(new Error('Not allowed by CORS'))
// }
// }
// }
// app.use('/api/v1/users',cors(corsOptions));
// app.use('/api/v1/sensors',cors(corsOptions));
app.use(function(req,res,next){
console.log(new Date().toUTCString()+' '+req.originalUrl);
next();
})
app.use('/api/*',expressJwt({
secret: utils.getSecret,
getToken: utils.getToken,
// isRevoked: function(req,payload,done){
// }
}).unless({ path: ['/api/v1/users/authenticate','/api/v1/users/register','/api/v1/measure', '/api/v1/sensors/data/byUser' ] }));
app.use(function (err, req, res, next) {
// if (err.name === 'UnauthorizedError') {
// // res.status(401).send('invalid token...');
// res.sendStatus(401);
// }
console.log(req.ip+' - ['+new Date().toUTCString()+'] '+err.name+' - '+
req.headers.host+' - "'+req.method+' '+req.originalUrl+'" - "'+
req.headers["user-agent"]+'"');
if(err.status!==undefined)
res.sendStatus(err.status);
else
res.sendStatus(500);
});
app.on('start', function () {
console.log('Application ready to serve requests.');
console.log('Environment: %s', app.kraken.get('env:env'));
});