From 41358d22267c3013315fc8bf76f0c46f45e7f8b9 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Wed, 22 Feb 2017 16:42:21 -0500 Subject: [PATCH] Adds ability to pass a middleware to CLI for instrumentation (#3554) * Adds ability to pass a middleware to CLI for instrumentation * Adds readme --- README.md | 1 + src/cli/definitions/parse-server.js | 3 +++ src/cli/parse-server.js | 14 +++++++++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 36adf09a..82f190e2 100644 --- a/README.md +++ b/README.md @@ -217,6 +217,7 @@ The client keys used with Parse are no longer necessary with Parse Server. If yo * `accountLockout` - Lock account when a malicious user is attempting to determine an account password by trial and error. * `passwordPolicy` - Optional password policy rules to enforce. * `customPages` - A hash with urls to override email verification links, password reset links and specify frame url for masking user-facing pages. Available keys: `parseFrameURL`, `invalidLink`, `choosePassword`, `passwordResetSuccess`, `verifyEmailSuccess`. +* `middleware` - (CLI only), a module name, function that is an express middleware. When using the CLI, the express app will load it just **before** mounting parse-server on the mount path. This option is useful for injecting a monitoring middleware. ##### Logging diff --git a/src/cli/definitions/parse-server.js b/src/cli/definitions/parse-server.js index 687cb547..5bbb6cde 100644 --- a/src/cli/definitions/parse-server.js +++ b/src/cli/definitions/parse-server.js @@ -249,4 +249,7 @@ export default { help: "Live query server configuration options (will start the liveQuery server)", action: objectParser }, + "middleware": { + help: "middleware for express server, can be string or function" + } }; diff --git a/src/cli/parse-server.js b/src/cli/parse-server.js index 8d6dbf82..7b6dd4ce 100755 --- a/src/cli/parse-server.js +++ b/src/cli/parse-server.js @@ -5,6 +5,7 @@ import definitions from './definitions/parse-server'; import cluster from 'cluster'; import os from 'os'; import runner from './utils/runner'; +const path = require("path"); const help = function(){ console.log(' Get Started guide:'); @@ -30,9 +31,20 @@ const help = function(){ function startServer(options, callback) { const app = express(); + if (options.middleware) { + let middleware; + if (typeof options.middleware == 'function') { + middleware = options.middleware; + } if (typeof options.middleware == 'string') { + middleware = require(path.resolve(process.cwd(), options.middleware)); + } else { + throw "middleware should be a string or a function"; + } + app.use(middleware); + } + const api = new ParseServer(options); const sockets = {}; - app.use(options.mountPath, api); const server = app.listen(options.port, options.host, callback);