lwsjs / local-web-server

A lean, modular web server for rapid full-stack development.
MIT License
1.22k stars 86 forks source link

How can I replace body content using a middleware? #154

Closed simplenotezy closed 4 years ago

simplenotezy commented 4 years ago

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) => {
            ctx.response.body = ctx.response.body.replaceAll('TEST', 'I-AM-REPLACED'); // cannot read property 'replaceAll' of undefined

            await next();
    }
  }
}

module.exports = ReplaceURLs

And this is how I run the stacks:

yarn ws -d dist --stack mw-replace-urls.js lws-static 

It will fail with:

TypeError: Cannot read property 'replaceAll' of undefined
    at /Users/mf/Projects/my-project/mw-replace-urls.js:5:42

What am I doing wrong?

75lb commented 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 .

simplenotezy commented 4 years ago

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?

simplenotezy commented 4 years ago

updated original post for clarification

75lb commented 4 years ago

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 .

simplenotezy commented 4 years ago

@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

simplenotezy commented 4 years ago

@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.

simplenotezy commented 4 years ago

@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?

75lb commented 4 years ago

I created an example project to demonstrate how response editing is done.. Any questions, let me know.

simplenotezy commented 4 years ago

Ah that's cool, thank you very much @75lb!

75lb commented 4 years ago

you're welcome