moleculerjs / moleculer-web

:earth_africa: Official API Gateway service for Moleculer framework
http://moleculer.services/docs/moleculer-web.html
MIT License
295 stars 118 forks source link

What is the benefit of using ctx.meta.$location & ctx.meta.$responseType vs just setting via ctx.meta.$responseHeaders? #350

Open valeeum opened 7 months ago

valeeum commented 7 months ago

I'm just curious why ctx.meta.$location & ctx.meta.$responseType options are offered as opposed to setting "Location" and "Content-Type" header via ctx.meta.$responseHeaders?

icebob commented 7 months ago

Nothing special just you can write the same shortly.

valeeum commented 7 months ago

But it's not just there for convenience since it can cause error if not set:

// Redirect
            if (res.statusCode==201 || (res.statusCode >= 300 && res.statusCode < 400 && res.statusCode !== 304)) {
                const location = ctx.meta.$location;
                /* istanbul ignore next */
                if (!location) {
                    this.logger.warn(`The 'ctx.meta.$location' is missing for status code '${res.statusCode}'!`);
                } else {
                    res.setHeader("Location", location);
                }
            }
valeeum commented 7 months ago

I suggest we use the following code:

const location = ctx.meta.$location ?? ctx.meta.$responseHeaders?.['Location'];

icebob commented 7 months ago

Good catch