-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentientmachinelabs-server.js
52 lines (42 loc) · 1.33 KB
/
sentientmachinelabs-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
'use strict';
var express = require('express'),
fs = require('fs'),
https = require('https'),
jwt = require('express-jwt'),
config, secretKey;
process.env.NODE_ENV = process.env.NODE_ENV || 'localdev';
config = require('./server/config/config');
secretKey = process.env.NODEJS_KEY;
if (!secretKey) {
throw new Error(`Provide NODEJS_KEY as environment variable.
Required for signing authorization tokens.`)
}
let app = express();
// todo: uncomment below after implementing authorization
/* app.use('/api', jwt({ secret: secretKey, algorithms: ['HS256'] }));
app.use(function (err, req, res, next) {
if (err.name === 'UnauthorizedError') {
res.sendStatus(401);
return;
}
next();
})
*/
require('./server/config/express')(app);
require('./server/routes')(app);
// ssl server for localdev, to support various security related headers in browser
// production should handle this differently
if (process.env.NODE_ENV === 'localdev') {
let sslOpts = {
key: fs.readFileSync('./ssl/localhost-key.pem', 'utf-8'),
cert: fs.readFileSync('./ssl/localhost.pem', 'utf-8')
}
https.createServer(sslOpts, app).listen(config.port);
}
else {
app.listen(config.port, config.ip, function () {
console.log(`Express server listening on ${config.ip}:${config.port},
in ${app.get('env')} mode.`)
});
}
module.exports = app;