<commit_before>'use strict';

var express = require('express');
var basicAuth = require('basic-auth');
var fs = require('fs');
var path = require('path');
var bodyParser = require('body-parser');
var api = require(path.join(__dirname, './api/api.js'));

var settings = JSON.parse(
  fs.readFileSync(path.join(__dirname, '../config/settings.json'), 'utf8'));
var port = process.env.PORT || settings.port;

var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var auth = function (req, res, next) {
  function unauthorized(res) {
    res.set('WWW-Authenticate', 'Basic realm="serviceBrokerAuth"');
    return res.sendStatus(401);
  };

  var user = basicAuth(req);
  if (!user || !user.name || !user.pass) {
    return unauthorized(res);
  };

  if (user.name === settings.user && user.pass === settings.password) {
    return next();
  } else {
    return unauthorized(res);
  };
  next();
};

app.use(auth);

var router = express.Router();
var serviceBrokerApi = new api();
router.get('/catalog', function(req, res) {
  res.json(serviceBrokerApi.getCatalog());
});

app.use('/v2', router);
var server = app.listen(port, function () {
    var port = server.address().port;
    console.log('Service broker app is running and listening at port %s', port);
});
module.exports = server;<commit_msg>servicebroker: Change the order to invoke express middleware<commit_after>'use strict';

var express = require('express');
var basicAuth = require('basic-auth');
var fs = require('fs');
var path = require('path');
var bodyParser = require('body-parser');
var api = require(path.join(__dirname, './api/api.js'));

var settings = JSON.parse(
  fs.readFileSync(path.join(__dirname, '../config/settings.json'), 'utf8'));
var port = process.env.PORT || settings.port;

var app = express();
var auth = function (req, res, next) {
  function unauthorized(res) {
    res.set('WWW-Authenticate', 'Basic realm="serviceBrokerAuth"');
    return res.sendStatus(401);
  };

  var user = basicAuth(req);
  if (!user || !user.name || !user.pass) {
    return unauthorized(res);
  };

  if (user.name === settings.user && user.pass === settings.password) {
    return next();
  } else {
    return unauthorized(res);
  };
  next();
};

var router = express.Router();
var serviceBrokerApi = new api();
router.get('/catalog', function(req, res) {
  res.json(serviceBrokerApi.getCatalog());
});


//define the sequence of middleware
app.use(auth);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/v2', router);

var server = app.listen(port, function () {
    var port = server.address().port;
    console.log('Service broker app is running and listening at port %s', port);
});
module.exports = server;
{'fraction_non_alphanumeric': 0.18106252258764005, 'fraction_numerical': 0.003614022406938923, 'mean_word_length': 4.481188118811882, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3365584', 'n_tokens_mistral': 874, 'n_tokens_neox': 861, 'n_words': 221}