Closed thescientist13 closed 5 years ago
Hi, this stack specifies that your middleware only is loaded (and nothing else):
$ ./node_modules/.bin/ws --stack middleware.js
Is that what you intended? Don't you at least need static file hosting downstream of your custom middleware? E.g.
$ ./node_modules/.bin/ws --stack middleware.js lws-static
Run this command to see the middleware options built into ws
, you can include any of these in your stack to create a custom server:
$ ws middleware-list
[ 'lws-basic-auth',
'lws-body-parser',
'lws-request-monitor',
'lws-log',
'lws-cors',
'lws-json',
'lws-compress',
'lws-rewrite',
'lws-blacklist',
'lws-conditional-get',
'lws-mime',
'lws-range',
'lws-mock-response',
'lws-spa',
'lws-static',
'lws-index' ]
Ah, I see.
So are you saying the lws.config.js file would not be respected in this case? Or do i need to "chain" stacks via the CLI instead? (or both?)
lws.config.js
options are respected unless overridden on the command line - CLI args take precedence..
Ah, ok I think I see what you mean now.
Is there a way to load a middleware via the stack
option in lws.config.js? e.g.
module.exports = {
...
stack: [
'body-parser',
'request-monitor',
'log',
'cors',
'json',
'rewrite',
'blacklist',
'conditional-get',
'mime',
'compress',
'mock-response',
'spa',
'static',
'index',
'my-middleware' // my-middleware.js
],
...
}
Thanks, so this gets the local web server running correctly now with my app and with my middleware loading (i know I can probably prune extra stacks I don't actually need)
./node_modules/.bin/ws --stack body-parser request-monitor log cors json rewrite blacklist conditional-get mime compress mock-response spa static index ./middleware.js
However, the return
function never executes?
module.exports = MiddlewareBase => class Example extends MiddlewareBase {
middleware (options) {
console.log('options', options); // this executes
return (ctx, next) => {
console.log('ctx', ctx); // this doesn't log???
};
}
};
This may be more in the realm of Koa at this point though, but any thoughts would be much appreciated!
it's because you have put your custom middleware right at the end of the stack - after static
and index
(one of which must be response middleware as it's terminating the request without forwarding it on to your custom middleware).
Great, changing the ordering of --stack
has my custom middleware running now. Still getting Not Found but I think I can take it from here now.
Thanks for all your help!
Was exploring the middleware docs to see if I could use it to set a cookie for all requests but was running into some issues when adding a
--stack
.What I have working was this:
Running it as
And everything is good ✅
When I try to add a simple middleware
All I get is a white page that says Not Found 🚫
If I follow the example from the docs, I can confirm that I see Hello on the page.
It does appear that I can see my middleware in the output from
ws
when using Chrome in Incognito mode to load the siteThanks for any help you may be able to lend! Let me know if you need anymore info