<commit_before>'use strict';

var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var mongoose = require('mongoose');

var authentication = require('./authentication.js');
var apiRoute = require('./routes/api.js');
var models = require('./models/models.js');
var errors = require('./errors.js');

var app = express();

var startServer = function () {
  var port = process.env.port || 8000;
  var listener = app.listen(port, function () {
    console.log('Server listening on port %d...', listener.address().port);
  });
};

var connectionString = process.env.MONGODB_CONNECTION_STRING || 'mongodb://127.0.0.1';
mongoose.connect(connectionString);

mongoose.connection.on('connected', function () {
  models.init(mongoose.connection);
  startServer();
});

var appFolder = process.argv[2] === 'dist' ? '.' : 'app';
if (process.env.NODE_ENV === 'production') {
  app.set('trust proxy', 1);
  appFolder = '.';
}
console.log('Serving from ' + appFolder + ' folder...');

// A workaround to to be able to use secure cookies in Azure:
// http://scottksmith.com/blog/2014/08/22/using-secure-cookies-in-node-on-azure/
app.use(function (req, res, next) {
  if (req.headers['x-arr-ssl'] && !req.headers['x-forwarded-proto']) {
    req.headers['x-forwarded-proto'] = 'https';
  }
  return next();
});

var rootPath = path.join(__dirname, '..', appFolder);
var staticFolder = '/static';

app.use(staticFolder, express.static(rootPath));
<commit_msg>Avoid starting server listener multiple times<commit_after>'use strict';

var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var mongoose = require('mongoose');

var authentication = require('./authentication.js');
var apiRoute = require('./routes/api.js');
var models = require('./models/models.js');
var errors = require('./errors.js');

var app = express();

var connectionString = process.env.MONGODB_CONNECTION_STRING || 'mongodb://127.0.0.1';
mongoose.connect(connectionString);

mongoose.connection.once('connected', function () {
  models.init(mongoose.connection);
  var port = process.env.port || 8000;
  var listener = app.listen(port, function () {
    console.log('Server listening on port %d...', listener.address().port);
  });
});

// Log errors from connection events to help debugging potential database
// connectivity issues.
var logError = function (eventName) {
  return function () {
    console.error('Received database ' + eventName + ' at ' + new Date().toISOString());
  };
};
mongoose.connection.on('disconnected', logError('disconnected'));
mongoose.connection.on('reconnected', logError('reconnected'));

var appFolder = process.argv[2] === 'dist' ? '.' : 'app';
if (process.env.NODE_ENV === 'production') {
  app.set('trust proxy', 1);
  appFolder = '.';
}
console.log('Serving from ' + appFolder + ' folder...');

// A workaround to to be able to use secure cookies in Azure:
// http://scottksmith.com/blog/2014/08/22/using-secure-cookies-in-node-on-azure/
app.use(function (req, res, next) {
  if (req.headers['x-arr-ssl'] && !req.headers['x-forwarded-proto']) {
    req.headers['x-forwarded-proto'] = 'https';
  }
  return next();
});

var rootPath = path.join(__dirname, '..', appFolder);
var staticFolder = '/static';

app.use(staticFolder, express.static(rootPath));

{'fraction_non_alphanumeric': 0.18714609286523215, 'fraction_numerical': 0.011325028312570781, 'mean_word_length': 4.689210950080516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2525989', 'n_tokens_mistral': 1123, 'n_tokens_neox': 1102, 'n_words': 249}