From 8259455ac5d8a9e0f8d4615cbb832a9e727eadfc Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Thu, 13 Nov 2014 19:15:50 +0300 Subject: [PATCH] switch to express 5 It's needed for the better separation between rest api and web interface. --- lib/index-web.js | 5 +++-- lib/index.js | 28 +++++++++++++++------------- package.yaml | 7 ++++++- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/index-web.js b/lib/index-web.js index c3df354e2..8793ec28a 100644 --- a/lib/index-web.js +++ b/lib/index-web.js @@ -4,6 +4,7 @@ var fs = require('fs') var marked = require('marked') var Handlebars = require('handlebars') var Error = require('http-errors') +var bodyParser = require('body-parser') var Search = require('./search') var Middleware = require('./middleware') @@ -12,7 +13,7 @@ module.exports = function(config, auth, storage) { var can = Middleware.allow(config) app.use(Cookies.express()) - app.use(express.urlencoded()) + app.use(bodyParser.urlencoded({ extended: false })) app.use(auth.cookie_middleware()) app.use(function(req, res, next) { // disable loading in frames (clickjacking, etc.) @@ -47,7 +48,7 @@ module.exports = function(config, auth, storage) { // Static app.get('/-/static/:filename', function(req, res, next) { var file = __dirname + '/static/' + req.params.filename - res.sendfile(file, function(err) { + res.sendFile(file, function(err) { if (!err) return; if (err.status === 404) { next() diff --git a/lib/index.js b/lib/index.js index bcc3ad45e..3fccd959e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,6 +2,8 @@ var Cookies = require('cookies') var express = require('express') var fs = require('fs') var Error = require('http-errors') +var expressJson5 = require('express-json5') +var compression = require('compression') var Auth = require('./auth') var Logger = require('./logger') var Config = require('./config') @@ -66,8 +68,8 @@ module.exports = function(config_hash) { }) app.use(Cats.middleware) app.use(auth.auth_middleware()) - app.use(express.json({ strict: false, limit: config.max_body_size || '10mb' })) - app.use(express.compress()) + app.use(expressJson5({ strict: false, limit: config.max_body_size || '10mb' })) + app.use(compression()) app.use(Middleware.anti_loop(config)) // validate all of these params as a package name @@ -382,16 +384,6 @@ module.exports = function(config_hash) { }) } - app.use(app.router) - app.use(function(err, req, res, next) { - if (typeof(res.report_error) !== 'function') { - // in case of very early error this middleware may not be loaded before error is generated - // fixing that - error_reporting_middleware(req, res, function(){}) - } - res.report_error(err) - }) - if (config.web && config.web.enable) { app.use(require('./index-web')(config, auth, storage)) } else { @@ -400,10 +392,20 @@ module.exports = function(config_hash) { }) } - app.use(function(req, res, next) { + app.get('/*', function(req, res, next) { next( Error[404]('file not found') ) }) + app.use(function(err, req, res, next) { + if (err.code === 'ECONNABORT' && res.statusCode === 304) return + if (typeof(res.report_error) !== 'function') { + // in case of very early error this middleware may not be loaded before error is generated + // fixing that + error_reporting_middleware(req, res, function(){}) + } + res.report_error(err) + }) + return app } diff --git a/package.yaml b/package.yaml index ee49dc47e..d81255b72 100644 --- a/package.yaml +++ b/package.yaml @@ -18,7 +18,12 @@ bin: sinopia: ./bin/sinopia dependencies: - express: '>=3.15.0 <4.0.0-0' + express: '>=5.0.0-0 <6.0.0-0' + + # express middlewares + express-json5: '>=0.1.0 <1.0.0-0' + body-parser: '>=1.9.2 <2.0.0-0' + compression: '>=1.2.0 <2.0.0-0' commander: '>=2.3.0 <3.0.0-0' js-yaml: '>=3.0.1 <4.0.0-0'