Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add res.json(...) support - /w work around #39

Open
bpetty-formfast opened this issue Dec 31, 2020 · 2 comments
Open

Add res.json(...) support - /w work around #39

bpetty-formfast opened this issue Dec 31, 2020 · 2 comments

Comments

@bpetty-formfast
Copy link

Almost all of my controllers return res.json(...).
run-middleware does not appear to correctly support this.

In express/lib/response.js it tries to see if Content-Type is set as a header, eventually through the getHeader(...) call.
getHeader is implemented a little poorly, expecting "this[kOutHeaders]" to equal null, but instead it is undefined.

That means all res.json(...) calls will fail. I haven't looked at your code, but is your response not extending OutgoingMessage ?

As a workaround, after I did runMiddleware(app), I added this little gem:

app.response.getHeader = (name) => { 
    return null; 
};`
@Aminadav
Copy link
Owner

If you wish the create a fix, I will review and merge it.

@Aminadav Aminadav reopened this Mar 13, 2021
@customautosys
Copy link

Facing same problem due to undefined content-type. I tried to add a content-type to the headers in createRes but that didn't work either. Also I'm having a problem where the IP cannot be passed to the other middleware that I'm calling even though I have passed in {ip:...} in the options of runMiddleware.

TypeError: Cannot read properties of undefined (reading 'startsWith')
    at getIPFromReq (/home/walfin/Dokumentujo/Gitlab/quasar_upgrade/caswebsite/node_modules/directus/dist/utils/get-ip-from-req.js:22:15)
    at handler (/home/walfin/Dokumentujo/Gitlab/quasar_upgrade/caswebsite/node_modules/directus/dist/middleware/authenticate.js:25:48)
    at /home/walfin/Dokumentujo/Gitlab/quasar_upgrade/caswebsite/node_modules/directus/dist/utils/async-handler.js:3:66
    at Layer.handle [as handle_request] (/home/walfin/Dokumentujo/Gitlab/quasar_upgrade/caswebsite/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/walfin/Dokumentujo/Gitlab/quasar_upgrade/caswebsite/node_modules/express/lib/router/index.js:328:13)
    at /home/walfin/Dokumentujo/Gitlab/quasar_upgrade/caswebsite/node_modules/express/lib/router/index.js:286:9
    at Function.process_params (/home/walfin/Dokumentujo/Gitlab/quasar_upgrade/caswebsite/node_modules/express/lib/router/index.js:346:12)
    at next (/home/walfin/Dokumentujo/Gitlab/quasar_upgrade/caswebsite/node_modules/express/lib/router/index.js:280:10)
    at extractToken (/home/walfin/Dokumentujo/Gitlab/quasar_upgrade/caswebsite/node_modules/directus/dist/middleware/extract-token.js:30:5)
    at Layer.handle [as handle_request] (/home/walfin/Dokumentujo/Gitlab/quasar_upgrade/caswebsite/node_modules/express/lib/router/layer.js:95:5)
node:_http_outgoing:654
  const entry = headers[StringPrototypeToLowerCase(name)];
                       ^

TypeError: Cannot read properties of undefined (reading 'content-type')
    at ServerResponse.getHeader (node:_http_outgoing:654:24)
    at ServerResponse.res.get (/home/walfin/Dokumentujo/Gitlab/quasar_upgrade/caswebsite/node_modules/express/lib/response.js:812:15)
    at ServerResponse.json (/home/walfin/Dokumentujo/Gitlab/quasar_upgrade/caswebsite/node_modules/express/lib/response.js:274:13)
    at /home/walfin/Dokumentujo/Gitlab/quasar_upgrade/caswebsite/node_modules/directus/dist/middleware/error-handler.js:93:20
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants