Not compatible with express serverMiddleware
See original GitHub issueWhen 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
- 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
- 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:
- Created 3 years ago
- Comments:10 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
@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 thereq
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
astrue
when it populatesreq.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. 🛏️ 🙏 🤣