diff --git a/lib/cartodb/server.js b/lib/cartodb/server.js index 12f3cf42..25193a7e 100644 --- a/lib/cartodb/server.js +++ b/lib/cartodb/server.js @@ -1,22 +1,24 @@ -const express = require('express'); -const cors = require('./middleware/cors'); -const user = require('./middleware/user'); -const bodyParser = require('body-parser'); const _ = require('underscore'); - -const StatsClient = require('./stats/client'); -const stats = require('./middleware/stats'); - +const express = require('express'); const { mapnik } = require('windshaft'); -const lzmaMiddleware = require('./middleware/lzma'); -const errorMiddleware = require('./middleware/error-middleware'); -const syntaxError = require('./middleware/syntax-error'); -const servedByHostHeader = require('./middleware/served-by-host-header'); +const jsonReplacer = require('./utils/json-replacer'); + const logger = require('./middleware/logger'); +const bodyParser = require('body-parser'); +const servedByHostHeader = require('./middleware/served-by-host-header'); +const stats = require('./middleware/stats'); +const lzmaMiddleware = require('./middleware/lzma'); +const cors = require('./middleware/cors'); +const user = require('./middleware/user'); const ControllersFactory = require('./controllers/factory'); +const syntaxError = require('./middleware/syntax-error'); +const errorMiddleware = require('./middleware/error-middleware'); + +const StatsClient = require('./stats/client'); + module.exports = function createServer (serverOptions) { validateOptions(serverOptions); @@ -27,21 +29,21 @@ module.exports = function createServer (serverOptions) { bootstrapFonts(serverOptions); - const app = bootstrap(serverOptions); + const app = express(); + + app.enable('jsonp callback'); + app.disable('x-powered-by'); + app.disable('etag'); + app.set('json replacer', jsonReplacer()); app.use(logger(serverOptions)); - app.use(bodyParser.json()); - app.use(servedByHostHeader()); - app.use(stats({ enabled: serverOptions.useProfiler, statsClient: global.statsClient })); - app.use(lzmaMiddleware()); - app.use(cors()); app.use(user()); @@ -73,40 +75,6 @@ function bootstrapFonts(opts) { } } -function bootstrap(opts) { - var app; - if (_.isObject(opts.https)) { - // use https if possible - app = express.createServer(opts.https); - } else { - // fall back to http by default - app = express(); - } - app.enable('jsonp callback'); - app.disable('x-powered-by'); - app.disable('etag'); - - // Fix: https://github.com/CartoDB/Windshaft-cartodb/issues/705 - // See: http://expressjs.com/en/4x/api.html#app.set - app.set('json replacer', function (key, value) { - if (value !== value) { - return 'NaN'; - } - - if (value === Infinity) { - return 'Infinity'; - } - - if (value === -Infinity) { - return '-Infinity'; - } - - return value; - }); - - return app; -} - function mapnikVersion(opts) { return opts.grainstore.mapnik_version || mapnik.versions.mapnik; } diff --git a/lib/cartodb/utils/json-replacer.js b/lib/cartodb/utils/json-replacer.js new file mode 100644 index 00000000..cccac1f6 --- /dev/null +++ b/lib/cartodb/utils/json-replacer.js @@ -0,0 +1,19 @@ +module.exports = function jsonReplacerFactory () { + // Fix: https://github.com/CartoDB/Windshaft-cartodb/issues/705 + // See: http://expressjs.com/en/4x/api.html#app.set + return function jsonReplacer (key, value) { + if (value !== value) { + return 'NaN'; + } + + if (value === Infinity) { + return 'Infinity'; + } + + if (value === -Infinity) { + return '-Infinity'; + } + + return value; + }; +};