From c52b98a08550e9f9460f9dbe261a734bb43b446a Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Ansari Date: Tue, 22 Sep 2020 13:47:32 +0530 Subject: [PATCH] add all HTTP verbs support for web APIs --- index.js | 35 +++++++++++++++++++++++++++++++++-- web/config.js | 3 ++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index ee3b7df..da6829e 100644 --- a/index.js +++ b/index.js @@ -71,8 +71,39 @@ module.exports = url => { // Host all Web API handlers config.webApis.forEach( - ({ url, handler }) => { - app.get( + ({ url, handler, verb }) => { + app.methodToBeUsed = undefined; + + switch (verb) { + case 'POST': + app.methodToBeUsed = app.post; + break; + case 'PUT': + app.methodToBeUsed = app.put; + break; + case 'PATCH': + app.methodToBeUsed = app.patch; + break; + case 'DELETE': + app.methodToBeUsed = app.delete; + break; + case 'HEAD': + app.methodToBeUsed = app.head; + break; + case 'OPTIONS': + app.methodToBeUsed = app.options; + break; + case 'CONNECT': + app.methodToBeUsed = app.connect; + break; + case 'TRACE': + app.methodToBeUsed = app.trace; + break; + default: + app.methodToBeUsed = app.get; + } + + app.methodToBeUsed( url, (req, res) => { res.send(handler({ diff --git a/web/config.js b/web/config.js index 3dc9836..0a80ac6 100644 --- a/web/config.js +++ b/web/config.js @@ -111,7 +111,8 @@ module.exports = { webApis: [ { url: '/api/time', - handler: () => new Date() + handler: () => new Date(), + verb: 'GET' } ], // Assets to be copied to the `public` directory, relative to this directory