require('dotenv').config();

var express = require('express');
var app = express();
var fs = require( 'fs' );
var auth = require('basic-auth');
var bodyParser = require('body-parser');

app.use(bodyParser.json({
    type: '*/*'
}));

//authorization
app.use(function(req, res, next) {
    var credentials = auth(req);
    if (!credentials || credentials.name !== process.env.API_USER || credentials.pass !== process.env.API_PASS) {
        res.set({
            'WWW-Authenticate': 'Basic realm="simple-admin"'
        });
        res.sendStatus(401);
    } else {
        next();
    }
});

routes = fs.readdirSync('./routes');
routes.forEach( function( routeFile ) {
    app.use('/api/' + routeFile, require('./routes/' + routeFile));
});

function error(err, req, res, next) {
    console.error(err.stack);
    res.send(500);
}
app.use(error);

var server = app.listen(process.env.RASPAPI_PORT, function() {

    var host = server.address().address;
    var port = server.address().port;

    console.log('raspapi listening at http://%s:%s', host, port);

});

@@ -179,16 +179,26 @@
 ser');%0A%0A
+//parsing%0A
 app.use(
@@ -595,16 +595,203 @@
  %7D%0A%7D);%0A%0A
+//pretty jsonresponses%0Aapp.use(function(req, res, next) %7B%0A    res.sendStatusJson = function (status) %7B%0A        res.status(status).send(%7Bstatus: status%7D);%0A    %7D;%0A    next();%0A%7D);%0A%0A//routes%0A
 routes =
@@ -932,24 +932,48 @@
 );%0A%7D);%0A%0A
+//errorhandling%0Aapp.use(
 function
  error(e
@@ -968,14 +968,8 @@
 tion
- error
 (err
@@ -1031,33 +1031,71 @@
 es.s
-end(500);%0A%7D%0Aapp.use(error
+tatus(500).send(%7B%0A        error: %22500%22%0A    %7D);%0A    next(err);%0A%7D
 );%0A%0A

improve error and "status code only" responses to return json