From 9883f460832d04eeb5ea00786e63c5851d269cce Mon Sep 17 00:00:00 2001 From: facascante Date: Tue, 7 Feb 2017 07:05:08 +0800 Subject: [PATCH 1/5] upgrade express to version 4+ --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 39696c9..3baf4f5 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "chai": "~1.6.1", "sinon-chai": "~2.4.0", "request": "~2.21.0", - "express": "~3.5.1", + "express": "~4.13.4", "ejs": "~1.0.0" }, "homepage": "https://github.com/likeastore/maintenance" From fe1a59d8aba88f66832a35ede235d6bb35b0bd4c Mon Sep 17 00:00:00 2001 From: facascante Date: Tue, 7 Feb 2017 07:06:05 +0800 Subject: [PATCH 2/5] remove configure remove configure --- test/app/app.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/app/app.js b/test/app/app.js index f3e217d..9223906 100644 --- a/test/app/app.js +++ b/test/app/app.js @@ -5,15 +5,13 @@ function create(options) { var called = {}; var app = express(); - app.configure(function () { - app.set('port', process.env.PORT || 3030); - app.set('views', __dirname + '/views'); - app.engine('html', require('ejs').renderFile); - }); + app.set('port', process.env.PORT || 3030); + app.set('views', __dirname + '/views'); + app.engine('html', require('ejs').renderFile); app.get('/', function (req, res) { called['/'] = true; - res.send(200); + res.sendStatus(200); }); app.get('/api/call', function (req, res) { From a6a1153f4d9f139b7f01154ed7b43f9406345f9c Mon Sep 17 00:00:00 2001 From: facascante Date: Tue, 7 Feb 2017 07:08:07 +0800 Subject: [PATCH 3/5] change direct app route definition to express.Router --- source/maintenance.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/source/maintenance.js b/source/maintenance.js index 6c7eac7..07f378f 100644 --- a/source/maintenance.js +++ b/source/maintenance.js @@ -1,3 +1,5 @@ +var express = require('express'); + function maintenance(app, options) { var mode = false, endpoint = false, @@ -38,15 +40,18 @@ function maintenance(app, options) { var server = function (app) { if (endpoint) { - app.post(url, checkAccess, function (req, res) { + var router = express.Router(); + router.post(url, checkAccess, function (req, res) { mode = true; - res.send(200); + res.sendStatus(200); }); - app.del(url, checkAccess, function (req, res) { + router.delete(url, checkAccess, function (req, res) { mode = false; - res.send(200); + res.sendStatus(200); }); + + app.use('/',router); } }; From 374b410c4d7a50d43887f7393e4150bd26abf93b Mon Sep 17 00:00:00 2001 From: Time Lord Date: Tue, 7 Feb 2017 07:26:20 +0800 Subject: [PATCH 4/5] fix test and upgrade code to express 4 --- package.json | 5 ++++- source/maintenance.js | 2 +- test/spec/maintenence.spec.js | 40 +++++++++++++++++++++-------------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 3baf4f5..31cdeff 100644 --- a/package.json +++ b/package.json @@ -35,5 +35,8 @@ "express": "~4.13.4", "ejs": "~1.0.0" }, - "homepage": "https://github.com/likeastore/maintenance" + "homepage": "https://github.com/likeastore/maintenance", + "dependencies": { + "sinon": "^1.17.7" + } } diff --git a/source/maintenance.js b/source/maintenance.js index 526f381..a299b11 100644 --- a/source/maintenance.js +++ b/source/maintenance.js @@ -35,7 +35,7 @@ function maintenance(app, options) { return next(); } - res.send(401); + res.sendStatus(401); }; var server = function (app) { diff --git a/test/spec/maintenence.spec.js b/test/spec/maintenence.spec.js index 5db0a98..2dc1e2d 100644 --- a/test/spec/maintenence.spec.js +++ b/test/spec/maintenence.spec.js @@ -85,12 +85,14 @@ describe('maintenance.js', function () { results = body; done(err); }); + + it('should return normal page', function () { + expect(response.statusCode).to.equal(200); + expect(results).to.equal('OK'); + }); }); - it('should return normal page', function () { - expect(response.statusCode).to.equal(200); - expect(results).to.equal('OK'); - }); + }); describe('put to maintenance', function () { @@ -122,12 +124,14 @@ describe('maintenance.js', function () { results = body; done(err); }); + + it('should return normal page', function () { + expect(response.statusCode).to.equal(200); + expect(results).to.equal('OK'); + }); }); - it('should return normal page', function () { - expect(response.statusCode).to.equal(200); - expect(results).to.equal('OK'); - }); + }); }); }); @@ -172,12 +176,14 @@ describe('maintenance.js', function () { results = body; done(err); }); + + it('should return normal page', function () { + expect(response.statusCode).to.equal(200); + expect(results).to.equal('OK'); + }); }); - it('should return normal page', function () { - expect(response.statusCode).to.equal(200); - expect(results).to.equal('OK'); - }); + }); }); }); @@ -252,12 +258,14 @@ describe('maintenance.js', function () { results = body; done(err); }); + + it('should return normal page', function () { + expect(response.statusCode).to.equal(200); + expect(results).to.equal('OK'); + }); }); - it('should return normal page', function () { - expect(response.statusCode).to.equal(200); - expect(results).to.equal('OK'); - }); + }); }); }); From d623da25beb9138509c4fa3fb8df48fbd499951d Mon Sep 17 00:00:00 2001 From: Time Lord Date: Mon, 13 Feb 2017 07:43:07 +0800 Subject: [PATCH 5/5] found an issue when trying maintenance enabled, it's no longer possible to disabled. i added a handle to make it possible --- source/maintenance.js | 17 +++++++++++++-- test/spec/maintenence.spec.js | 40 +++++++++++++++++++---------------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/source/maintenance.js b/source/maintenance.js index a299b11..a498ef5 100644 --- a/source/maintenance.js +++ b/source/maintenance.js @@ -47,6 +47,7 @@ function maintenance(app, options) { }); router.delete(url, checkAccess, function (req, res) { + mode = false; res.sendStatus(200); }); @@ -56,6 +57,7 @@ function maintenance(app, options) { }; var handle = function (req, res) { + var isApi = api && req.url.indexOf(api) === 0; res.status(status); @@ -69,10 +71,21 @@ function maintenance(app, options) { var middleware = function (req, res, next) { if (mode) { - return handle(req, res); + var request_url = req.url.split("?")[0]; + if(request_url === url && ["DELETE","POST"].indexOf(req.method) != -1){ + next(); + } + else{ + return handle(req, res); + } + + + } + else{ + next(); } - next(); + }; var inject = function (app) { diff --git a/test/spec/maintenence.spec.js b/test/spec/maintenence.spec.js index 2dc1e2d..d27fb8e 100644 --- a/test/spec/maintenence.spec.js +++ b/test/spec/maintenence.spec.js @@ -86,10 +86,11 @@ describe('maintenance.js', function () { done(err); }); - it('should return normal page', function () { - expect(response.statusCode).to.equal(200); - expect(results).to.equal('OK'); - }); + }); + + it('should return normal page', function () { + expect(response.statusCode).to.equal(200); + expect(results).to.equal('OK'); }); @@ -125,10 +126,11 @@ describe('maintenance.js', function () { done(err); }); - it('should return normal page', function () { - expect(response.statusCode).to.equal(200); - expect(results).to.equal('OK'); - }); + }); + + it('should return normal page', function () { + expect(response.statusCode).to.equal(200); + expect(results).to.equal('OK'); }); @@ -177,10 +179,11 @@ describe('maintenance.js', function () { done(err); }); - it('should return normal page', function () { - expect(response.statusCode).to.equal(200); - expect(results).to.equal('OK'); - }); + }); + + it('should return normal page', function () { + expect(response.statusCode).to.equal(200); + expect(results).to.equal('OK'); }); @@ -249,7 +252,9 @@ describe('maintenance.js', function () { describe('and return back to normal', function () { beforeEach(function (done) { - request.del(url + '/maintenance?access_key=secret', done); + request.del(url + '/maintenance?access_key=secret', function(err,resp,body){ + done(err,body); + }); }); beforeEach(function (done) { @@ -259,13 +264,12 @@ describe('maintenance.js', function () { done(err); }); - it('should return normal page', function () { - expect(response.statusCode).to.equal(200); - expect(results).to.equal('OK'); - }); }); - + it('should return normal page', function () { + expect(response.statusCode).to.equal(200); + expect(results).to.equal('OK'); + }); }); }); });