style(server): server setup cleanup
This commit is contained in:
parent
443a481487
commit
21c957cf0f
1 changed files with 15 additions and 18 deletions
33
lib/app.js
33
lib/app.js
|
|
@ -47,10 +47,9 @@ module.exports = function (options) {
|
|||
|
||||
app.use(express.bodyParser());
|
||||
|
||||
if (!options.https) {
|
||||
server = http.createServer(app);
|
||||
}
|
||||
else {
|
||||
var authMiddleware;
|
||||
|
||||
if (options.https) {
|
||||
var serverOptions = {
|
||||
key: options.https.key,
|
||||
cert: options.https.cert,
|
||||
|
|
@ -61,30 +60,28 @@ module.exports = function (options) {
|
|||
};
|
||||
|
||||
server = https.createServer(serverOptions, app);
|
||||
}
|
||||
|
||||
var authMiddleware = function (req, res, next) {
|
||||
req.device = {};
|
||||
return next();
|
||||
};
|
||||
|
||||
if (options.https) {
|
||||
authMiddleware = function(req, res, next) {
|
||||
var fingerprint = req.connection.getPeerCertificate().fingerprint;
|
||||
|
||||
config.isAuthorized(fingerprint, function (err, device) {
|
||||
if (err) {
|
||||
config.isAuthorized(routes.getFingerprint(req), function (err, device) {
|
||||
if (err) {
|
||||
res.json({err: 'Internal Server Error'});
|
||||
return next(err);
|
||||
}
|
||||
if (!device) {
|
||||
res.statusCode = 404;
|
||||
res.json({err: 'Not Found'});
|
||||
return next(new Error('Device is unpaired'));
|
||||
res.json(404, {err: 'Not Found'});
|
||||
return next(new Error('Device is unpaired'));
|
||||
}
|
||||
next();
|
||||
});
|
||||
};
|
||||
|
||||
} else {
|
||||
server = http.createServer(app);
|
||||
|
||||
authMiddleware = function (req, res, next) {
|
||||
req.device = {};
|
||||
return next();
|
||||
};
|
||||
}
|
||||
|
||||
routes.init(app, config, trader, authMiddleware);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue