<commit_before>var express = require('express'),
    app = module.exports = express(),
    config = require('../../lib/config'),
    path = require('path'),
    fs = require('fs'),
    Bundles = require('../bundles');

app.set('views', process.cwd());

app.get('/viewsetup.js', function(req, res) {
    // Do some entirely silly string operations to extract the bundle name from the referer and pass it to the API setup script
    var referer = req.headers.referer;
    var start = referer.indexOf('/view/') + 6;
    var end = referer.indexOf('/', start + 1);
    var bundleName = referer.substring(start, end);

    res.type('.js');
    res.render('lib/includes/viewsetup.js.ejs', {host: config.host, port: config.port, bundleName: bundleName});
});

app.get('/view/:bundle_name*', function(req, res) {
    var bundleName = req.param('bundle_name');

    Bundles.find(bundleName)
        .then(function(bundle) {
            // We start out assuming the user is trying to reach the index page
            var resName = 'index.html';

            /*  We need a trailing slash for view index pages so that relatively linked assets can be reached as expected.
             This behaves a bit weird in that it adds two trailing slashes, this appears to be because both '' and '/'
             don't count as params. Luckily, the fact that we end up with '//' doesn't cause any problems. */
            if (!req.params[0]) {
                res.redirect(req.url + '/');
            }

            // If the first param isn't just a slash, the user must be trying to resolve an asset and not the index page
            if (req.params[0] !== '/' && req.params[0] !== '//') {
                resName = req.params[0].substr(1);
            }

            var fileLocation = path.join(__dirname, '../../', bundle.dir, 'view/', resName);

            // Check if the file exists
            if (!fs.existsSync(fileLocation)) {
                res.status(404).send('Unable to find '+ bundle.dir +'/'+ resName);
            }

            res.sendFile(fileLocation);
        }, function (err) {
            res.status(404).send('Unable to find bundle \''+ bundleName +'\'');
        });
});
<commit_msg>Fix double forward slash in view URLs<commit_after>var express = require('express'),
    app = module.exports = express(),
    config = require('../../lib/config'),
    path = require('path'),
    fs = require('fs'),
    Bundles = require('../bundles');

require('string.prototype.endswith');

app.set('views', process.cwd());

app.get('/viewsetup.js', function(req, res) {
    // Do some entirely silly string operations to extract the bundle name from the referer and pass it to the API setup script
    var referer = req.headers.referer;
    var start = referer.indexOf('/view/') + 6;
    var end = referer.indexOf('/', start + 1);
    var bundleName = referer.substring(start, end);

    res.type('.js');
    res.render('lib/includes/viewsetup.js.ejs', {host: config.host, port: config.port, bundleName: bundleName});
});

app.get('/view/:bundle_name*', function(req, res) {
    var bundleName = req.param('bundle_name');

    Bundles.find(bundleName)
        .then(function(bundle) {
            // We start out assuming the user is trying to reach the index page
            var resName = 'index.html';

            /*  We need a trailing slash for view index pages so that relatively linked assets can be reached as expected.
             This behaves a bit weird in that it adds two trailing slashes, this appears to be because both '' and '/'
             don't count as params. Luckily, the fact that we end up with '//' doesn't cause any problems. */
            if (!req.params[0] && !req.url.endsWith('/')) {
                res.redirect(req.url + '/');
            }

            // If the first param isn't just a slash, the user must be trying to resolve an asset and not the index page
            if (req.params[0] !== '/' && req.params[0] !== '//') {
                resName = req.params[0].substr(1);
            }

            var fileLocation = path.join(__dirname, '../../', bundle.dir, 'view/', resName);

            // Check if the file exists
            if (!fs.existsSync(fileLocation)) {
                res.status(404).send('Unable to find '+ bundle.dir +'/'+ resName);
            }

            res.sendFile(fileLocation);
        }, function (err) {
            res.status(404).send('Unable to find bundle \''+ bundleName +'\'');
        });
});

{'fraction_non_alphanumeric': 0.14694699172444645, 'fraction_numerical': 0.005815253858197271, 'mean_word_length': 2.297935103244838, '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': '5597885', 'n_tokens_mistral': 1279, 'n_tokens_neox': 1235, 'n_words': 406}