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.

Cannot read properties of undefined (reading 'content-type') using express js

See original GitHub issue

Environment

  • Operating System: Linux
  • Node Version: v16.10.0
  • Nuxt Version: 3.0.0-27265876.3cd4494
  • Package Manager: yarn@1.22.17
  • Bundler: Vite
  • User Config: serverMiddleware
  • Runtime Modules: -
  • Build Modules: -

Describe the bug

serverMiddleware doesn’t work with express.js (4.17.1) and throws “Cannot read properties of undefined (reading 'content-type')” error

Reproduction

Clone a repository with the bug and run it as dev server

$ git clone https://github.com/martiliones/nuxt-express-bug
$ cd nuxt-express-bug && yarn install
$ yarn dev

Open http://localhost:3000/projects/ in your browser

Additional context

I tested serverMiddleware with h3 and it works just fine:

// api/index.js
import { createApp } from 'h3'

const app = createApp()
app.use('/projects', () => JSON.stringify([
    {
      name: 'asdsad',
      type: ['asdsad', 'asdsad']
    }
]))

Logs

Cannot read properties of undefined (reading 'content-type')
  at ServerResponse.getHeader (node:_http_outgoing:597:24)  
  at ServerResponse.res.get (./node_modules/express/lib/response.js:789:15)  
  at ServerResponse.json (./node_modules/express/lib/response.js:263:13)  
  at file://./.nuxt/nitro/index.mjs:139:7  
  at Layer.handle [as handle_request] (./node_modules/express/lib/router/layer.js:95:5)  
  at next (./node_modules/express/lib/router/route.js:137:13)  
  at Route.dispatch (./node_modules/express/lib/router/route.js:112:3)  
  at Layer.handle [as handle_request] (./node_modules/express/lib/router/layer.js:95:5)  
  at ./node_modules/express/lib/router/index.js:281:22  
  at Function.process_params (./node_modules/express/lib/router/index.js:335:12)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:25 (10 by maintainers)

github_iconTop GitHub Comments

7reactions
danielroecommented, Jan 20, 2022

Using the full URL prevents Nitro from making a local call. So it’s a workaround with a performance cost.

4reactions
calumkcommented, Dec 12, 2021

Using the direct full URL eg

http://localhost:3000/api/whatever

Instead of

api/whatever

Worked for me …

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: cannot read property of undefined (ExpressJS/POST)
I have a node js app in which I cannot print the input text from a form, using body-parser. My index.ejs: <form id="demo-2"...
Read more >
Uncaught TypeError: Cannot read property of undefined In
Uncaught TypeError : Cannot read property of undefined error occurs in Chrome when you read a property or call a method on an...
Read more >
Cannot Read Property of Undefined in JavaScript - Rollbar
TypeError : Cannot read property of undefined occurs when a property is read or a function is called on an undefined variable.
Read more >
5.x API - Express.js
A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body ), or an empty...
Read more >
[Solved]-Why does the error occur "Cannot read properties of ...
... the error occur "Cannot read properties of undefined (reading 'title')"-node.js. ... Since this is a regular form post, it will using the...
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