question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not compatible with express serverMiddleware

See original GitHub issue

When a custom serverMiddleware is added that exports an express instance, all @nuxt/content client-side requests hang rather than resolving.

Version

@nuxt/content: v1.4.1 nuxt: v2.13.3

Reproduction link

https://codesandbox.io/s/nuxtcontent-demo-x11rc

Steps to reproduce

  1. Create serverMiddleware that exports an express instance that uses json middleware, for example:
    import express from 'express'
    const app = express()
    // Note that without following line this bug is not reproduced
    app.use(express.json())
    app.get('/api/test', (req, res) => res.end())
    export default app
    
  2. Add it to nuxt.config.js:
    serverMiddleware: [require.resolve("./api/test.js")],
    

What is expected?

I expect adding additional endpoints as above should not interfere with the endpoint @nuxt/content adds.

What is actually happening?

All @nuxt/content client-side requests hang rather than resolving.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
NozomuIkutacommented, Feb 22, 2021

@Atinux

Since “done is better than perfect”, I submitted a PR where Nuxt Content gets request body by an option, whose default value is 'body' (i.e. req.body), assuming body-parser is already used in the pipeline.

User can override this option so that Nuxt Content can be agnostic on what server middleware consumes read stream and which property is populated.

Please see the PR for more detail.

1reaction
NozomuIkutacommented, Feb 13, 2021

@Atinux @danielroe

I’ve looked into this issue to have a fun on weekend.

If I understand it correctly, the root cause is that express.json() (i.e. body-parser package, or raw-body package more accurately) consumes all the stream before the req is passed to Nuxt Content’s middleware.

So, the event listeners below are never called, and consequently the promise remains pending forever:

https://github.com/nuxt/content/blob/ebaedede5935f597fe325322ef57f09f6222e1ff/packages/content/lib/middleware.js#L19-L28

This promise stuck can be avoided by adding a condition to if-block, because body-parser sets req._body as true when it populates req.body:

if (['POST', 'PUT'].includes(req.method) && !req._body) { /* ... */ }

But, this solution would not be appropriate because now Nuxt Content becomes wary about behavior of package(s) that it doesn’t depend on. Perhaps, do you think it’s fine only if we provide some guide in documentation?

By the way, there might be more information that I have not yet found, but I would stopped research here since it’s over 1:30 a.m. in Japan. I think I should go to bed for health, rather than sticking to have a fun. 🛏️ 🙏 🤣

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running an Express server middleware alongside Nuxt
I run my files with npm run dev and my express files are in /middleware . The endpoint which I'm trying to fetch...
Read more >
servermiddleware routes are not recognized #1486 - GitHub
This is because in module.exports we already specified the "global" prefix as /api for this whole express router instance. The above setup works ......
Read more >
The serverMiddleware Property - Nuxt
The serverMiddleware property. Define server-side middleware. Type: Array. Items: String or Object or Function. Nuxt internally creates a connect instance ...
Read more >
Writing middleware for use in Express apps
End the request-response cycle. Call the next middleware in the stack. If the current middleware function does not end the request-response cycle, it...
Read more >
Meme Overflow a Twitter: "'express' not working in server ...
'express' not working in server middleware Nuxt js https://stackoverflow.com/questions/65700873/806889… #bodyparser #nodejs #vuejs #express #nuxtjs.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found