-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
50 lines (41 loc) · 1.14 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
'use strict';
const SwaggerExpress = require('swagger-express-mw');
const app = require('express')();
module.exports = app; // for testing
const jwt = require('./api/helpers/jwt.js');
const config = {
appRoot: __dirname, // required config
swaggerSecurityHandlers: {
UserSecurity: function (req, authOrSecDef, scopesOrApiKey, cb) {
jwt.verifyToken(scopesOrApiKey)
.then(decoded => {
req.jwt = {
username: decoded.username
};
cb(null);
})
.catch(cb);
}
}
};
/** DB */
const db = require('./models/db.js');
db.connect().then(() => {
console.log('db connected')
},
e => console.log(e));
/** Error handler */
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Server error.');
});
SwaggerExpress.create(config, function(err, swaggerExpress) {
if (err) { throw err; }
// install middleware
swaggerExpress.register(app);
const port = process.env.PORT || 10010;
app.listen(port);
if (swaggerExpress.runner.swagger.paths['/hello']) {
console.log('try this:\ncurl http://127.0.0.1:' + port + '/hello?name=Scott');
}
});