<commit_before>var express = require('express');
var config = require('konfig')();
var glob = require('glob');
var redis = require('redis');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');

var app = express();

// Allow headers
app.use(function (req, res, next) {

    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', config.app.frontend);

    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);

    // Pass to next layer of middleware
    next();
});
//TODO create the client using the actual host when connected to dev or prod, do this by adding the host and port into the createclient() function

app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());


//get and parse cookie and place it into req.cookies
app.use(cookieParser());
if(process.env.NODE_ENV == "dev" || process.env.NODE_ENV == "prod" || process.env.NODE_ENV == "mike"){
    app.use("/login", require(process.cwd()+"/routes/login"));
}
else {
    app.get("/testRoute/", function (req, res) {
        var client = redis.createClient();
        client.set("foobarbaz", "test");
        res.cookie("IMPId", "foobarbaz", { maxAge: 24 * 60 * 60 * 1000, domain: config.app.domain, httpOnly: true });
        res.send("success");
    });
}

//These routes shouldnt be cookie tested
app.use("/login", require(process.cwd()+"/routes/login"));
app.use("/Login/confirmUser", require(process.cwd()+"/routes/Login/confirmUser"));
app.use("/Login/createUser", require(process.cwd()+"/routes/Login/createUser"));
app.use("/Login/testLookup", require(process.cwd()+"/routes/Login/testLookup"));


//Middleware for verifying a user is logged in before hittin a route
app.use(function(req,res,next)
{
    var port=config.app.redis.port;
    var host=config.app.redis.host;
    var client = redis.createClient(port,host);
    console.log(req.cookies.IMPId);
    client.exists(req.cookies.IMPId, function(err, reply) {
        if(reply == 1) {
            console.log("Successfully Authenticated!");
            next();
        }
        else{
            //TODO Have the navigation object on the login page with a window alert if this happens
            console.log("Oops, something went wrong with authentication!");
            res.status(404).send("User not Found");
        }
    });
});

//Adds all the routes by path to the app
var path = process.cwd()+'/routes';
glob.sync('**/*.js',{'cwd':path}).forEach(
    function(file){
        var ns = '/'+file.replace(/\.js$/,'');
        if(ns != "/login" && ns != "/Login/confirmUser" && ns != "/Login/createUser" && ns != "/Login/testLookup") {
            app.use(ns, require(path + ns));
        }
    }
);


app.use('*', function(req, res){
    console.log("Error trying to display route: "+req.path);
    res.status(404).send("Nothing Found");
});


app.listen(config.app.port);


console.log('Express server running on port '+config.app.port);
<commit_msg>Merging and properly instantiating and closing out client connections in the redis object<commit_after>var express = require('express');
var config = require('konfig')();
var glob = require('glob');
var redis = require('redis');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');

var app = express();

// Allow headers
app.use(function (req, res, next) {

    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', config.app.frontend);

    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);

    // Pass to next layer of middleware
    next();
});

app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());


//get and parse cookie and place it into req.cookies
app.use(cookieParser());

//These routes shouldnt be cookie tested
app.use("/login", require(process.cwd()+"/routes/login"));
app.use("/Login/confirmUser", require(process.cwd()+"/routes/Login/confirmUser"));
app.use("/Login/createUser", require(process.cwd()+"/routes/Login/createUser"));
app.use("/Login/testLookup", require(process.cwd()+"/routes/Login/testLookup"));


//Middleware for verifying a user is logged in before hittin a route
app.use(function(req,res,next)
{
    var port=config.app.redis.port;
    var host=config.app.redis.host;
    var client = redis.createClient(port,host);
    console.log(req.cookies.IMPId);
    client.exists(req.cookies.IMPId, function(err, reply) {
        if(reply == 1) {
            console.log("Successfully Authenticated!");
            next();
        }
        else{
            //TODO Have the navigation object on the login page with a window alert if this happens
            console.log("Oops, something went wrong with authentication!");
            res.status(404).send("User not Found");
        }
    });
});

//Adds all the routes by path to the app
var path = process.cwd()+'/routes';
glob.sync('**/*.js',{'cwd':path}).forEach(
    function(file){
        var ns = '/'+file.replace(/\.js$/,'');
        if(ns != "/login" && ns != "/Login/confirmUser" && ns != "/Login/createUser" && ns != "/Login/testLookup") {
            app.use(ns, require(path + ns));
        }
    }
);


app.use('*', function(req, res){
    console.log("Error trying to display route: "+req.path);
    res.status(404).send("Nothing Found");
});


app.listen(config.app.port);


console.log('Express server running on port '+config.app.port);

{'fraction_non_alphanumeric': 0.1656141458793811, 'fraction_numerical': 0.003789074834227976, 'mean_word_length': 3.426974143955276, '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': '426587', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1915, 'n_words': 516}