<commit_before>'use strict';

const express = require('express');
const bodyParser = require('body-parser');
const error = require('http-errors');
const uuid = require('uuid');
const db = require('./lib/db');
const friends = require('./lib/store/friends');
const subscriptions = require('./lib/store/subscriptions');
const blockList = require('./lib/store/block-list');

const app = module.exports = express();

app.use(bodyParser.json());

app.post('/friends', (req, res, next) => {
    const emails = req.body.friends;

    if (!emails) {
        return next(error.BadRequest('Missing friends data'));
    }
    if (emails.length != 2) {
        return next(error.BadRequest('Exactly two emails are required'));
    }

    friends.connect(emails)
        .then(() => res.status(200).json({success: true}))
        .catch(err => next(err));
});

app.post('/friends/list', (req, res, next) => {
    const email = req.body.email;

    friends.list(email)
        .then(friends => {
            res.status(200).json({
                success: true,
                friends: friends.map(f => f.email),
                count: friends.length
            });
        })
        .catch(err => next(err));
});

app.post('/friends/common', (req, res, next) => {
    const emails = req.body.friends;

    friends.findCommon(emails)
        .then(friends => {
            res.status(200).json({
                success: true,
                friends: friends.map(f => f.email),
                count: friends.length
            });
        })
        .catch(err => next(err));
});

app.post('/subscriptions', (req, res, next) => {
    const requestor = req.body.requestor;
    const target = req.body.target;

    if (!req.body.target) {
        return next(error.BadRequest('Target parameter is missing'));
    }
    if (!req.body.requestor) {
        return next(error.BadRequest('Requestor parameter is missing'));
    }

    subscriptions.subscribe(requestor, target)
        .then(() => res.status(200).json({success: true}))
        .catch(err => next(err));
});

app.post('/block', (req, res, next) => {
    const requestor = req.body.requestor;
    const target = req.body.target;

    if (!req.body.target) {
        return next(error.BadRequest('Target parameter is missing'));
    }
    if (!req.body.requestor) {
        return next(error.BadRequest('Requestor parameter is missing'));
    }

    blockList.add(requestor, target)
        .then(() => res.status(200).json({success: true}))
        .catch(err => next(err));
});

// 404 handler. We want a JSON response, so override express' default which renders HTML.
app.use((req, res, next) => {
    next(error.NotFound());
});

// Error handler. Constructs json response from given error objects statusCode and message 
// properties. If no statusCode and/or message are set, generates a generic 500 error.
app.use((err, req, res, next) => {
    const status = err.statusCode || 500;
    const message = err.message || 'Unknown error';

    if (status >= 500) {
        // try err stack first, some errors don't print stack trace by default
        console.error(err.stack || err);
    }

    res.status(status).json({error: message});
});<commit_msg>Refactor routes into seperate file<commit_after>'use strict';

const express = require('express');
const error = require('http-errors');
const bodyParser = require('body-parser');
const routes =  require('./lib/routes');

const app = module.exports = express();

app.use(bodyParser.json());

app.use(routes);

// 404 handler. We want a JSON response, so override express' default which renders HTML.
app.use((req, res, next) => {
    next(error.NotFound());
});

// Error handler. Constructs json response from given error objects statusCode and message 
// properties. If no statusCode and/or message are set, generates a generic 500 error.
app.use((err, req, res, next) => {
    const status = err.statusCode || 500;
    const message = err.message || 'Unknown error';

    if (status >= 500) {
        // try err stack first, some errors don't print stack trace by default
        console.error(err.stack || err);
    }

    res.status(status).json({error: message});
});
{'fraction_non_alphanumeric': 0.15936826992103373, 'fraction_numerical': 0.00957166786312515, 'mean_word_length': 2.8703703703703702, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '2411888', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1193, 'n_words': 333}