Closed simplenotezy closed 4 years ago
try placing your custom middleware *before lws-static in the --stack
list.. order is significant in a middleware chain
On Fri, 25 Sep 2020, 13:19 Mattias Fjellvang, notifications@github.com wrote:
I am trying to chain two middlewares - the native built in one, and a custom one I made to replace some text in the response body.
mw-replace-urls.js
class ReplaceURLs { middleware () { return async (ctx, next) => { console.log('I am here'); ctx.response.body = ctx.response.body.replaceAll('TEST', 'I-AM-REPLACED');
await next(); }
} }
module.exports = ReplaceURLs
And this is how I run the stacks:
yarn ws -d dist --stack lws-static mw-replace-urls.js
However, when I check console, I don't see any console messages. The site has been rendered, but nothing changed.
What am I doing wrong?
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/lwsjs/local-web-server/issues/154, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJQV3FGXGV2TI2KMTUT5TTSHSDDVANCNFSM4RZPFXFQ .
thanks @75lb Okay, so I guess the main issue was that I was loading mw-replace-urlsjs after lws-static, and it should be before. Anyhow. Now my issue is that ctx.response.body
is undefined, despite the content rendering just fine. Why is ctx.resposne.body
undefined?
updated original post for clarification
see lws-body-parser..
On Fri, 25 Sep 2020, 13:23 Mattias Fjellvang, notifications@github.com wrote:
updated original post for clarification
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/lwsjs/local-web-server/issues/154#issuecomment-698898071, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJQV3DGHRDBPGGMS7ZVFA3SHSDUFANCNFSM4RZPFXFQ .
@75lb Thanks for so prompt replies. I have tried looking at https://github.com/koajs/bodyparser and also replacing rawBody like this:
ctx.response.rawBody = ctx.response.rawBody.replace('sense', 'I-AM-REPLACED');
But doesn't seem to work.
Update:
Tried running this as wel:
yarn ws -d dist --stack lws-body-parser mw-replace-urls.js lws-static
No effect
@75lb I have tried many different variations, and also read https://github.com/lwsjs/local-web-server/wiki/How-to-access-the-body-of-an-incoming-request (although not very documented). I'm stuck at the moment unfortunately.
@75lb I might need to use:
app.use(bodyParser({ "enableTypes": ['json', 'form', 'text'], }));
Somewhere in my middleware to allow rawBody
to be present, but now sure where/how I would add this in my middelware, given that it is a native module. Any suggestions?
I created an example project to demonstrate how response editing is done.. Any questions, let me know.
Ah that's cool, thank you very much @75lb!
you're welcome
I am trying to chain two middlewares - the native built in one, and a custom one I made to replace some text in the response body.
mw-replace-urls.js
And this is how I run the stacks:
It will fail with:
What am I doing wrong?