krakenjs / meddleware Goto Github PK
View Code? Open in Web Editor NEWMiddleware configuration for express.
License: Other
Middleware configuration for express.
License: Other
Currently, middleware modules are specified like this:
{
"custom": {
"enabled": true,
"priority": 30,
"route": "/foo",
"module": "./lib/middleware",
"factoryMethod": "customMiddleware",
"arguments": [ "foo", { "bar": "baz" } ]
}
}
This issue proposes to switch to using strings for simple exported middleware, or objects
for including additional information:
{
"custom": {
"enabled": true,
"priority": 30,
"route": "/foo",
"module": "./lib/middleware"
}
}
{
"custom": {
"enabled": true,
"priority": 30,
"route": "/foo",
"module": {
"name": "./lib/middleware",
"method": "customMiddleware",
"arguments": [ "foo", { "bar": "baz" } ]
}
}
}
This is easy with a default of false:
{
"enable_something": "env:SOME_ENV_VAR|b",
"meddleware": {
"something": {
"enabled": "config:enable_something"
}
}
}
But how can it be arranged to default to true? This is potentially a confit/shortstop thing more than a meddleware thing, though I can see changes to any of them to fix it.
tryRequire
just calls require.resolve
which will, for local files, start relative to the file that contains it.
Therefore, if you try to require a file with a relative path that could resolve to a similarly named file from the util
folder in meddleware, ๐ฅ.
Proof:
$ echo "console.log('i am required');" > rq.js
$ cat - > index.js <<EOF
const meddleware = require('meddleware');
const express = require('express');
const app = express();
const config = {
example: {
module: './rq.js'
}
};
app.use(meddleware(config));
EOF
$ node index.js
/cat/city/secret/path/node_modules/meddleware/index.js:100
throw new Error('Unable to locate middleware in ' + config.name);
^
Error: Unable to locate middleware in ./rq.js
config package made its returned objects sealed on version 3.0. This breaks meddleware
:
$ node maps.js
/tmp/map-server/node_modules/meddleware/lib/util.js:50
obj && (obj.name = name);
^
TypeError: Cannot add property name, object is not extensible
at nameObject (/tmp/map-server/node_modules/meddleware/lib/util.js:50:22)
at /tmp/map-server/node_modules/meddleware/lib/util.js:38:16
at Array.map (<anonymous>)
at Object.mapValues (/tmp/map-server/node_modules/meddleware/lib/util.js:36:29)
at Function.onmount (/tmp/map-server/node_modules/meddleware/index.js:196:14)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:121:20)
at Function.emit (events.js:211:7)
at Function.<anonymous> (/tmp/map-server/node_modules/express/lib/application.js:238:8)
at Array.forEach (<anonymous>)
There is a work around given on this config issue, however having immutable and sealed objects on require('config')
is desirable.
Do you think meddleware
can do something about it?
Has something changed between meddleware 3.0.2 and meddleware 4.0 that would prevent a simple use case from working? I'm reading through the documentation and do not see anything that would indicate that this is the case.
I just spent a good amount of time tracking down the source of a problem that was preventing an application's routes from being loaded via meddleware / express-enrouten.
In my use case, meddleware's app.once('mount', function() { ... })
never ends up firing. Here is an example. Run it with:
$ export NODE_PATH=$NODE_PATH:./lib && node ./bin/index
In previous versions of kraken, I set the "module" property in the session config to specify a custom module to use to create session stores. With meddleware, it seems I need to instantiate the store myself at runtime and tack it on to the sessions config before the session store is created. Is that right, or is there another way to do this?
var PostgresStore = require('./lib/postgres-sessions');
app.on('middleware:before:sessions', function(args) {
args.config.store = new PostgresStore;
});
Trying to ensure a middleware does not get executed in a few routes (/developer/login, /developer/coverage) where the requestURI is "/developer". I specify "route" property for the middleware as "/(?!login|coverage)" which yields the following error:
Tue, 03 Jun 2014 15:19:52 GMT uncaughtException Invalid regular expression: /^/developer/(?:?!login|coverage)/?(?=/|$)/: Nothing to repeat
I've tried other permutations (?!(login|coverage)), etc. which either yield the same error or the middleware never runs on any route.
-Chris
Should now be serve-favicon
Line 31 in 56b600f
How can i do this in meddleware?
var express = require('express')
, i18n = require('i18next');
i18n.init({
saveMissing: true,
debug: true
});
var app = express();
app.use(i18n.handle);
i18n.registerAppHelper(app);
closed: won't matter for confit since they're independent steps. NM.
the module
property can either be shorthanded to a string or an object with name
and arguments
properties. We should normalize this so, i.e., confit can override arguments even if an upstream config uses the shorthand.
In other words:
{
"myMiddleware": {
"module": "./lib/myMiddleware"
}
}
app.on('middleware:after:myMiddleware', function (data) {
assert(data.spec.module.name === './lib/myMiddleware');
});
I have a module where it's exported as:
module.exports = {
middleware : function (req, res, next) { ... }
};
If I use require
and register in code, I can do something like:
var middleware = require('someModule').middleware;
app.use('/', middleware);
However, with meddleware, how would I specify it?
"someModule": {
"enabled": true,
"priority": 1,
"module" : {
"name": "someModule",
"method": "middleware"
}
},
method
doesn't work because it expects it to be a constructor function.
Kraken by default has an enabled MemoryStore session. I don't want this, but I can't put my session config in json. I need to basically stop Kraken/meddleware from making anything about the session and then app.use at the right time (middleware:after:cookieParser). Right now I'm deleting that with "delete config.get('middleware').session" in onconfig. Whatever the right way is to do something like this, it should be clearly documented.
I am trying to write typescript definitions and the large configuration object makes it difficult to do interfaces.
It seems wether a middleware is enabled or not is checked for each request and hence the module must always be loaded from the start. Since I guess most people use this module with krakenjs which comes with a lot of preconfigured modules it would be nice to disable the ones you don't want/need completely without having to fork or assign them to some noop module.
Personally I don't see the need to be able to enable/disable modules at runtime, but maybe there's a special use case that I don't know of. Restarting a node process is not particularly slow/expensive imho and createToggleWrapper
itself is somewhat buggy with anonymous functions since it uses eval
.
Is it me or all the middleware:before
events are triggered after the middleware is loaded? I can't find a way where the before
events are fired before registering, which, from testing, is in line with after
events.
I know it is possible to register middleware functions based on the route. But how do I register middleware functions based on the same route with different HTTP methods.
For example,
/api GET will have a different set of middleware.
/api POST will have a different set of middleware.
I've noticed the following transient failure during a test of the Parallel middleware configuration:
# parallel
ok 100 (unnamed assert)
ok 101 (unnamed assert)
ok 102 (unnamed assert)
ok 103 no response error
ok 104 response is defined
ok 105 response body is defined
ok 106 (unnamed assert)
not ok 107 (unnamed assert)
---
operator: ok
expected: true
actual: false
...
Which can be traced to ./test/meddleware.js:505
:
time = Date.now();
req('/', function () {
time = Date.now() - time;
t.ok(time > 1450);
t.ok(time < 1550); // XXX: not ok 107 (unnamed assert)
t.end();
});
Some printf debugging reveals the average time is ~1536 ms [n=200, min: 1515 ms, max: 1557 ms] on my Windows machine (~1518 ms on my Macbook). About 1 in 50 tests fail (2%) on my Windows machine with a time of greater than 1550 ms. Obviously this is jitter in the v8 timers on my work machine, but I've found that bumping the time requirement to 1560 to be sufficient (n=200 runs).
Here.
prop
corresponds to an object key which is just a string.
Hi,
I'm developing a middleware to authenticate routes and also have a callback url after authentication.
I would like to add that callback route as part of middleware file
something like this.
app.get('/authenticateCallbackurl',function(){
// redirect
})
Also do you have custom meddleware examples
Unconfirmed however I got a report from @lmarkus that the enabled
toggle is broken for middlewares defined in flow-control sections starting with v3.x.
According to the documentation, the module.name
option is required, which makes sense. However, in Kraken's implementation, I see this:
"router": {
"module": {
"arguments": [{ "directory": "path:./controllers" }]
}
}
Could someone please provide some insight into what's going on here? To what module are the arguments that we see here being passed?
Since kraken still uses ^1 it would be nice to get that feature to developers.
Hey,
Assuming this is our config:
...
"my-middleware": {
"priority": 2,
"module": {
"name": "./foo.js",
"method": "makeMiddleware"
}
}
...
And this is foo.js
:
module.exports = {
makeMiddleware: function(callback) {
// init code ...
callback(function(req, res, next) {
// whatever
});
}
}
As you can see, makeMiddleware
is an asynchronous function, which yields to a callback. This can happen because sometime middleware initialization can do async stuff (like reading from a config file, database, etc.). Unfortunately, from looking at the docs and code, it seems that there is no support for async middleware factories, so the code above will fail.
Can we add support for that? ๐
When installed with NPM5 the "other" user lacks read permissions on this file:
node_modules/meddleware/index.js
This is similar to the issue that was noted for the array-unique
module here:
jonschlinkert/array-unique#5
I created a yo kraken basic app to demonstrate. I added a splitTraffic middleware item to config.json and it prints out if it is ever called. It activates on the route /add (assuming I got the regex syntax straight).
Running the app and hitting /add should print the message from the middleware and the message from the route controller. All I see is the route controller. If I remove, the route on the middleware, then both show up.
Here is the sample showing it.
https://github.com/rragan/testroute
I am trying but failing to register PassportJS middleware in the config file. Here is the config file code for meddleware:
// passportjs
"passport": {
"enabled": true,
"priority": 92,
"module": {
"name": "passport",
"method": "initialize"
}
},
// passportjs
"passport": {
"enabled": true,
"priority": 93,
"module": {
"name": "passport",
"method": "session"
}
}
What am I doing wrong ?
RQ
seems to have been written before libraries such as Bluebird
were available. Since Meddleware
isn't using one of the core features of RQ
(timeouts), wouldn't it be better to adopt a more standard approach to handling asynchronous actions?
lol. whoops.
Is it possible to pass an object to the middleware via the meddleware config?
The middleware that I'm trying to implement is jwt-redis-session. Its constructor takes in a Redis Client object.
Hi,
I'm trying to register the comression middleware with krakenjs and meddleware.
https://github.com/expressjs/compression
I cant' however figure out how to register and override the "filter" property, which need a "a filtering callback function".
Is there a way to provide such a setting via meddleware ?
We assume it is, but it's not. Can also be an array.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.