NodeJS - TypeError: Can not read property 'get' of undefined

1

I'm a beginner in NodeJs and I came across this problem when I was exporting a module: "TypeError: Can not read property 'get' of undefined" . I did not understand why it gave the error since the code is identical to the one of the course teacher, but still the error occurs. Currently the code looks 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, thank you right away:)

    
asked by anonymous 17.12.2018 / 17:43

1 answer

0

You need to set 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')
  })
}
    
17.12.2018 / 17:51