diff options
Diffstat (limited to 'app.js')
-rw-r--r-- | app.js | 71 |
1 files changed, 35 insertions, 36 deletions
@@ -1,65 +1,67 @@ 'use strict'; -var express = require('express'); -var logger = require('morgan'); -var bodyParser = require('body-parser'); -var bootlint = require('bootlint'); +const express = require('express'); +const logger = require('morgan'); +const bodyParser = require('body-parser'); +const bootlint = require('bootlint'); - -var HTML_MIME_TYPES = [ +const HTML_MIME_TYPES = [ 'text/html', 'application/xhtml+xml' ]; + // For context, unminified bootstrap.css + bootstrap.js is ~200KiB, // and JSFiddle inlines the contents of the CSS and JS panes of its editor into the resulting HTML. -var MAX_HTML_SIZE = '1MB'; +const MAX_HTML_SIZE = '1MB'; function disabledIdsFor(req) { - var rawIds = req.query.disable; + const rawIds = req.query.disable; if (!rawIds) { return []; } + return rawIds.split(','); } function lintsFor(html, disabledIds) { - var lints = []; - var reporter = function (lint) { - var output = false; + const lints = []; + const reporter = lint => { + let output = false; if (lint.elements && lint.elements.length) { - var elements = lint.elements; + const {elements} = lint; lint.elements = undefined; - elements.each(function (_, element) { + elements.each((_, element) => { if (element.startLocation) { - var locatedLint = Object.assign({}, lint); + const locatedLint = {...lint}; locatedLint.location = element.startLocation; lints.push(locatedLint); output = true; } }); } + if (!output) { lint.elements = undefined; lints.push(lint); } }; + bootlint.lintHtml(html, reporter, disabledIds); return lints; } -var routes = express.Router(); +const routes = express.Router(); // eslint-disable-line new-cap -routes.get('/', function (req, res) { +routes.get('/', (req, res) => { res.status(200).json({ status: 200, message: 'Bootlint is online!' }); }); -routes.post('/', function (req, res) { - var isHtml = HTML_MIME_TYPES.some(function (type) { - return req.is(type); - }); +routes.post('/', (req, res) => { + const isHtml = HTML_MIME_TYPES.some(type => req.is(type)); + if (!isHtml) { res.status(415).json({ status: 415, @@ -70,14 +72,12 @@ routes.post('/', function (req, res) { } res.format({ - 'application/json': function () { - var disabledIds = disabledIdsFor(req); - var html = req.body; - // console.log('HTML: ', html); - var lints = lintsFor(html, disabledIds); + 'application/json'() { + const disabledIds = disabledIdsFor(req); + const lints = lintsFor(req.body.html, disabledIds); res.status(200).json(lints); }, - 'default': function () { + default() { res.status(406).json({ status: 406, message: 'Not Acceptable', @@ -87,13 +87,12 @@ routes.post('/', function (req, res) { }); }); - -var app = express(); +const app = express(); app.use(logger('dev')); -HTML_MIME_TYPES.forEach(function (type) { +HTML_MIME_TYPES.forEach(type => { app.use(bodyParser.text({ - type: type, + type, limit: MAX_HTML_SIZE })); }); @@ -101,8 +100,8 @@ HTML_MIME_TYPES.forEach(function (type) { app.use('/', routes); // catch 404 and forward to error handler -app.use(function (req, res, next) { - var err = new Error('Not Found'); +app.use((req, res, next) => { + const err = new Error('Not Found'); err.status = 404; next(err); }); @@ -113,17 +112,18 @@ app.use(function (req, res, next) { // will print stacktrace /* eslint-disable no-unused-vars */ -app.use(function (err, req, res, next) { - var isHttpErr = Boolean(err.status); +app.use((err, req, res, next) => { + const isHttpErr = Boolean(err.status); if (!isHttpErr) { err.status = 500; } - var errJson = { + const errJson = { status: err.status, message: err.message }; + if (!isHttpErr) { errJson.stack = err.stack; } @@ -132,5 +132,4 @@ app.use(function (err, req, res, next) { }); /* eslint-enable no-unused-vars */ - module.exports = app; |