Nodejs - Typeerror: Cannot read Property 'get' of Undefined

Asked

Viewed 4,034 times

1

I am beginner in Nodejs and I came across this problem when I was exporting a module: "Typeerror: Cannot read Property 'get' of Undefined". I did not understand why the error occurred since the code is identical to the teacher of the course, but still the error occurs. Currently the code is like this:

module.exports = function(app){
  app.get('/noticias', (req, res)=>{
    res.render('noticias/noticias')
  })
}

When I do the require of the module the server does not go up, giving this error that I commented. I hope you can help me, I thank you already :)

  • Where are you making the call from that route you are setting?

1 answer

0


You need to define app (which is an instance of express), example:

let express = require('express');
let app = express();

app.get('/noticias', (req, res)=>{
  res.render('noticias/noticias')
})

What people usually do is also define app as global:

// arquivo1.js
let express = require('express');
global.app = express();

To use:

// arquivo2.js
require('arquivo1.js')
module.exports = function(app){
  app.get('/noticias', (req, res)=>{
     res.render('noticias/noticias')
  })
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.